MATLAB Answers

How can I find the max value and location within a matrix

52 views (last 30 days)
I am working with a 7783x1 matrix, and I am trying to find the max value & its location. But I need to do it in the script and not in the command window. I have the matrix data saved as a FILE.mat in the same folder.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 23 Jan 2021
Try this, where m is your matrix
maxValue = max(m(:));
rows = find(m == maxValue); % Find all locations where m equals the max value of m

  5 Comments

Show 2 older comments
zachary laird
zachary laird on 23 Jan 2021
Its been about 5 years since I have touched matlab but here is a photo of what is happening. In the current folder is "pothole.mat". It was a text file that I imported into matlab and it converted it to a numerical matrix (7781x1) and saved as what is shown.
Image Analyst
Image Analyst on 23 Jan 2021
You did not change the names like I said. Leave the semicolon off line 17 and see what fields it shows in the command window and use that instead of m.
zachary laird
zachary laird on 23 Jan 2021
I see where I need to change the name now. Thank you for your help.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 23 Jan 2021
[maxval, idx] = max(M);

  2 Comments

Image Analyst
Image Analyst on 23 Jan 2021
Only finds the FIRST occurrence, not all of them. See how I did it if you want to make sure you find all of them.
Adam Danz
Adam Danz on 23 Jan 2021
That's true, but I'd recommend using logical indexing rather than slowing it down with find()
maxval = max(M);
idx = M==maxval;

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!