a =
81 91 27 96 95
90 63 54 15 48
12 9 95 97 80
now we want to locate the maximum value 97. There are several approaches.
- Use max only.
>> [row_val row_ind] =max(a, [], 1)The maximum value is at [row_ind(col_ind), col_ind]
row_val =
90 91 95 97 95
row_ind =
2 1 3 3 1
>> [col_val col_ind] =max(row_val)
col_val =
97
col_ind =
4
- Use find.
>> [r c] =find(a==max(a(:)))
r =
3
c =
4
- Use single index.
>> [s_v s_i] =max(a(:))
s_v =
97
s_i =
12
>> [r c] =ind2sub(size(a), s_i)
r =
3
c =
4
*******************************************
Usually, to find the maximum value of a matirx, the following command is used:
max(a(:)) where a is a 2D matrix
The problem is that if the matrix size is too big, you may get yourself involved in a problem that is ??? Matrix is too large to convert to linear index. This is caused by a(:).
Try to use max(max(a)) instead.
Don't know what version of MATLAB you are using, but the first way should be generally MORE efficient. MATLAB won't resize the array, just write a temporary new header (<200 bytes) and then work its way down the data. Using max(max)) creates an intermediate temp. vector with length the number of columns -- which could be large.
ReplyDelete--Loren
The Art of MATLAB
Thank you for your comment. I use the 2006b version. When I run the normalized cuts segmentation (code is downloaded from http://www.cis.upenn.edu/~jshi/software/), I met the problem. So I use max(max(a)) instead of max(a(:)), it works well.
ReplyDeleteI had the same problem! Thanks for posting the solution!
ReplyDeleteFinding a max value in a matrix: i have been searching for this info. your works are indeed appreciated.
ReplyDelete---Simaneka, CPUT
I was actually looking for the reverse problem, how to avoid using max(max(a)).
ReplyDeleteThanks guys!
I would like ot know how to find a position of maximum value in matrix? Thank you in advance.
ReplyDelete[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.
ReplyDeletewenbin thank you very much
ReplyDeleteHow to check whether a number is member of a vector?
ReplyDeleteTo check whether a number is member of a vector, you can use function 'find'. Refer to the help document for details.
ReplyDeletewenbin said, "[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."
ReplyDeleteThis did not work for me or I do not understand your explanation. I have a 4x10 matrix A, and I need to find the indices of the largest value in this matrix. Can you please explain in more detail how I can get the matrix indices of the matrix A element which has the max value? e.g. A(2,3) has the max value.
See the post for details.
ReplyDeleteThanks wenbin! I see it now. It works for my large 256 x 16 x 60 matrix! Wow!
ReplyDeleteI really appreciate you work!! it was too nice!!
ReplyDeletebe careful when you use the "find" technique. When two or more entries have the same maximum number, it will return an array instead of r and c. So it's better to use the first solution.
ReplyDeleteit was too nice!!
ReplyDeletehow can I find the maximum with the last indiecs?
ReplyDeletePlease read the post carefully. The approaches should work.
Delete