anon
anon

Reputation: 697

MATLAB: What's [Y,I]=max(AS,[],2);?

I just started matlab and need to finish this program really fast, so I don't have time to go through all the tutorials.

can someone familiar with it please explain what the following statement is doing.

[Y,I]=max(AS,[],2);

The [] between AS and 2 is what's mostly confusing me. And is the max value getting assigned to both Y and I ?

Upvotes: 3

Views: 2154

Answers (5)

Artelius
Artelius

Reputation: 49089

C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.

Also, the [C, I] = max(...) form gives you the maximum values in C, and their indices (i.e. locations) in I.

Why don't you try an example, like this? Type it into MATLAB and see what you get. It should make things much easier to see.

m = [[1;6;2] [5;8;0] [9;3;5]]
max(m,[],2)

Upvotes: 2

shabbychef
shabbychef

Reputation: 1999

note the apparent wrinkle in the matlab convention; there are a number of builtin functions which have signature like:

xs = sum(x,dim)

which works 'along' the dimension dim. max and min are the oddbal exceptions:

xm = max(x,dim);     %this is probably a silent semantical error!
xm = max(x,[],dim);  %this is probably what you want

I sometimes wish matlab had a binary max and a collapsing max, instead of shoving them into the same function...

Upvotes: 0

Eugene Yokota
Eugene Yokota

Reputation: 95624

According to the reference manual,

C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.

[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.

I think [] is there just to distinguish itself from max(A,B).

Upvotes: 5

mjv
mjv

Reputation: 75205

AS is matrix.
This will return the largest elements of AS in its 2nd dimension (i.e. its columns)

Upvotes: 2

Neosani
Neosani

Reputation: 172

This function is taking AS and producing the maximum value along the second dimension of AS. It returns the max value 'Y' and the index of it 'I'.

Upvotes: 1

Related Questions