Friday, December 08, 2006

MATLAB: find the maximum in a matrix

Given a matrix a,
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)
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
The maximum value is at [row_ind(col_ind), col_ind]
  • 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.

18 comments:

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

    --Loren
    The Art of MATLAB

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

    ReplyDelete
  3. I had the same problem! Thanks for posting the solution!

    ReplyDelete
  4. Finding a max value in a matrix: i have been searching for this info. your works are indeed appreciated.
    ---Simaneka, CPUT

    ReplyDelete
  5. I was actually looking for the reverse problem, how to avoid using max(max(a)).
    Thanks guys!

    ReplyDelete
  6. I would like ot know how to find a position of maximum value in matrix? Thank you in advance.

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

    ReplyDelete
  8. wenbin thank you very much

    ReplyDelete
  9. How to check whether a number is member of a vector?

    ReplyDelete
  10. To check whether a number is member of a vector, you can use function 'find'. Refer to the help document for details.

    ReplyDelete
  11. wenbin 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."
    This 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.

    ReplyDelete
  12. See the post for details.

    ReplyDelete
  13. Thanks wenbin! I see it now. It works for my large 256 x 16 x 60 matrix! Wow!

    ReplyDelete
  14. I really appreciate you work!! it was too nice!!

    ReplyDelete
  15. be 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.

    ReplyDelete
  16. it was too nice!!

    ReplyDelete
  17. how can I find the maximum with the last indiecs?

    ReplyDelete
    Replies
    1. Please read the post carefully. The approaches should work.

      Delete