Finding the highest mountain peak

10 views (last 30 days)
Brantosaurus on 29 Sep 2022
Commented: Brantosaurus on 30 Sep 2022
How might i find the highest mountain peak, and its name, from a defined set of named mountains?
I have data for a set of 10 mountains as 4 matrices.
[X] and [Y] are single row and column matrices defining distances along orthogonal horizontal coordinates.
[Z] is a 3D matrix (:,:,i) containing corresponding x,y height values and an index i=1:10 to define the mountain set.
[Name] is a 10 element 1D text matrix of of the mountain names.
Using a for loop I can plot the surfaces, find the peaks of each mountain and their names:
How might i find the overall highest peak, and its name in one hit (without the loop)?

Accepted Answer

KSSV on 29 Sep 2022
Edited: KSSV on 29 Sep 2022
[val,idx] = max(Z,[],3) ;
Get the maximum along third dimension, idx gives you the thirs index where max occurs. Now find the max of val, get it's index.
Or you ccan also use.
A = rand(3,3,3);
[M,I] = max(A(:));
[I_row, I_col, I_page] = ind2sub(size(A),I)
  1 Comment
Brantosaurus on 30 Sep 2022
It was the linear (column) index idx that was causing my confusion. Your second example using ind2sub sorted it out. Just what i needed. Much appreciated. :)

Sign in to comment.

More Answers (2)

Brantosaurus on 29 Sep 2022
Thanks. The below worked to get the overall highest peak:
[gvalue,gindex]=max(value); #gvalue is correct
Struggling with the name index! Tried to find correct 'position' with:
but it doesn't work. Included is val and idx matrix if it helps.

Image Analyst
Image Analyst on 29 Sep 2022
Why can't you just do this to find the row and column of the max for each of the 10 slices of data:
for k = 1 : size(Z, 3)
% Extract this mountain set.
thisMountainSet = Z(:, :, k);
% Find the max value of it
maxValue(k) = max(thisMountainSet(:));
% Find row(s) and column(s) where it occurs.
[rowsOfMax, colsOfMax] = find(thisMountainSet == maxValue(k));
% Now do something with this information....
Brantosaurus on 30 Sep 2022
Had a play with your script. I now get your point! Working in the loop you suggest on a step-by-step basis looks the safest option to identify potential multi peaks and their locations.
I managed to produce a summary after the loop using:
[M] = max(maxValue(:));
[I] = find(maxValue == M);
It appears to find the (overall) max and case(s) where they occur to refer back as necessary.
Many thanks for the advice. Much appreciated.

Sign in to comment.


More Answers in the  ThingSpeak Community



Community Treasure Hunt

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

Start Hunting!