0

MATLAB documentation says

"[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I."

But that does not seem to work. See below.

X = [2 8 4; 7 3 9];

[a,b] = max(X)

a =

 7     8     9

b =

 2     1     2

The indices could have been given as linear indices b = [ 2 3 6] so that X(b) would give the max values of columns of X in a.

It looks like an error in MATLAB because the doc says something but it does something different.

3 Answers3

2

If you give max a matrix, it finds the maximum of each column. So $a = [7,8,9]$ and $b = [2,1,2]$ because the maximum of the first column is $7$, found in the second row, the maximum of the second column is $8$, found in the first row, and the maximum of the third column is $9$, found in the second row.

Robert Israel
  • 448,999
0

As it is stated at Robert Israel's answer, max only returns the indeces for the columns. This is illustrated for $3\times3$ matrix as below:

$\begin{bmatrix}1 & 1 & 1\\ 2 & 2 & 2\\ 3 & 3 & 3 \end{bmatrix}$

If you want to get indeces of the actual matrix, you need a workaround. If you run the following code X(b) gives the maximum values for each column.

m=size(X,1);
for i=2:size(b,2)
b(i)=b(i)+m;
m=m+size(X,1);
end

You can make your own max function by developing this code.

  • My question is about another way of using max to get the indices as stated in matlab documentation (use "doc max" to get the documentation). Many other commands similar to max give both the values and the indices correctly. Even max is giving the indices but wrongly as given clearly in my question. Code can be written but my question is about why max is giving the wrong indices. – Seetha Rama Raju Sanapala Sep 03 '14 at 09:53
  • Just giving the row number of the element is of no use. In this kind of situation for other commands, MATLAB gives linear indexing numbers so that with a single index the element can be unambiguously identified. Kindly look at linear indexing in MATLAB. – Seetha Rama Raju Sanapala Sep 03 '14 at 09:57
  • "max" is not giving wrong results. You misunderstand the "doc max". And, I don't understand what you mean by linear indexing. I just propose a way to obtain b = [ 2 3 6] in case you need. –  Sep 03 '14 at 10:09
  • This is linear indexing taken from MATLAB documentation itself. And this is the right way of indexing in this kind of situations because you can get back your values from this indices. If we get the row numbers alone, what use is it? – Seetha Rama Raju Sanapala Sep 03 '14 at 10:17
  • "You can refer to the elements of a MATLAB matrix with a single subscript, A(k). MATLAB stores matrices and arrays not in the shape that they appear when displayed in the MATLAB Command Window, but as a single column of elements. This single column is composed of all of the columns from the matrix, each appended to the last.

    So, matrix A A = [2 6 9; 4 2 8; 3 5 1] A = 2 6 9 4 2 8 3 5 1 is actually stored in memory as the sequence 2, 4, 3, 6, 2, 5, 9, 8, 1

    – Seetha Rama Raju Sanapala Sep 03 '14 at 10:24
  • The element at row 3, column 2 of matrix A (value = 5) can also be identified as element 6 in the actual storage sequence. To access this element, you have a choice of using the standard A(3,2) syntax, or you can use A(6), which is referred to as linear indexing."
    The element at row 3, column 2 of matrix A (value = 5) can also be identified as element 6 in the actual storage sequence. To access this element, you have a choice of using the standard A(3,2) syntax, or you can use A(6), which is referred to as linear indexing."
    – Seetha Rama Raju Sanapala Sep 03 '14 at 10:24
  • There is a lot more information on linear indexing in MATLAB, too big to fit in1 or 3 comment boxes. I have just given here snippets. I have given the info in 3 comment boxes because I do not know another way of giving this in one box. Sorry for my ignorance if there is another way. – Seetha Rama Raju Sanapala Sep 03 '14 at 10:28
  • Thank you I understand linear indexing now. But my code above also gives the linear indexing results. I just didn't know the name of such indexing. I can't catch your problem about that. –  Sep 03 '14 at 10:28
  • No, we can write the code. That is not the problem. MATLAB should actually give the linear indices answers as it does in other commands in this kind of situations. We should notify MATLAB and get its response. May be, I am missing some thing. May be, they can improve it. We will know. – Seetha Rama Raju Sanapala Sep 03 '14 at 10:30
  • I raised the question at MATLAB Central : - the conclusion/suggestion was that a bug report can be generated. http://www.mathworks.com/matlabcentral/answers/153279-finding-the-max-values-and-their-coordinates-indices#answer_150598 – Seetha Rama Raju Sanapala Sep 04 '14 at 03:23
0

There is no error. The documentation is very clear about what it returns. If the linear indices are what you want, then you can get them with something like the following:

maxinds = sub2ind( size(X), b, [1:size(X,2)] );
  • More discussion on the error at MATLAB CENTRAL http://www.mathworks.com/matlabcentral/answers/153279-finding-the-max-values-and-their-coordinates-indices#answer_150598 – Seetha Rama Raju Sanapala Sep 04 '14 at 04:53