tags:

views:

99

answers:

5

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 ?

+1  A: 

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'.

Neosani
+3  A: 

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).

eed3si9n
+1  A: 

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

mjv
+1  A: 

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)
Artelius
A: 

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...

shabbychef

related questions