Automatically selecting peaks from a surf plot an creating new variables for them
Show older comments
Hello
I have a 100x100 numeric variable, which looks like this when plotted on a surf plot:

What I'm looking for, is a way to automatically create some new variables, each containing only one of the peaks, with the rest of the values deleted, as shown below :

Ideally I would like this to work for any number of peaks (generating one new variable for each of them),
and if possible, ignoring peaks below a certain threshold would be nice.
Thank you for your time!
Answers (1)
Prateekshya
on 7 Nov 2023
Edited: Prateekshya
on 7 Nov 2023
Hi Aristarchos,
As per my understanding, you want to extract the peak values from a 3D surface plot.
The closest workaround is to find the maximum values in the data from which the figure is generated. You can use the following functions.
- "max" function: This function can find the position of the maximum value in the data. For more information follow this link: https://in.mathworks.com/help/matlab/ref/max.html
[maxValue, maxIndex] = max(Z(:)); % Gives the maximum value and the corresponding index
[row, col] = ind2sub(size(Z), maxIndex); % Gives the row and column for the position
- "find" function: This function can find the position of all the values above a given threshold. For more information follow this link: https://in.mathworks.com/help/matlab/ref/find.html
[row, col] = find(Z > threshold); % Gives the rows and columns for the corresponding positions
peakValues = Z(sub2ind(size(Z), row, col)); % Gives the exact values
- "imregionalmax": This function to find regional maxima in the surface data. For more information follow this link: https://in.mathworks.com/help/images/ref/imregionalmax.html
binaryImage = imregionalmax(Z); % Gives a matrix indicating 1 at the positions of maximum values
[row, col] = find(binaryImage); % Gives the set of rows and columns for the corresponding positions
peakValues = Z(sub2ind(size(Z), row, col)); % Gives the exact values
"Z" is the matrix containing the surface data.
If you only have the figure and not the data, you can follow this to extract the data from the figure: https://in.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures
I hope it helps!
1 Comment
Aristarchos Mavridis
on 9 Nov 2023
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!