find peaks in 2d array ans plot as surface in matlab

You can use the 'imregionalmax' function in MATLAB to find the local maxima of a 2D array. Once you have the indices of the peaks, you can create a surface plot using the 'surf' function.

Here is an example code snippet:

main.m
% Example 2D array
array = [1 1 1 1 1; 
         1 2 5 2 1; 
         1 2 4 2 1; 
         1 1 1 1 1];

% Find the peaks
peaks = imregionalmax(array);

% Get the indices of the peaks
[rows, cols] = find(peaks);

% Create the surface plot
[X, Y] = meshgrid(1:size(array, 2), 1:size(array, 1));
Z = array;
figure;
surf(X, Y, Z);

hold on;
% Plot the peaks as red dots on the surface
plot3(cols, rows, Z(sub2ind(size(array), rows, cols)), 'r.', 'MarkerSize', 20);
hold off;

% Set labels and title
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Surface plot with peaks');
568 chars
29 lines

In this example, we create a 2D array called 'array' and find the local maxima using 'imregionalmax'. Then, we get the indices of the peaks using 'find'. Finally, we create a surface plot using 'surf' and plot the peaks on the surface using 'plot3'. The 'hold on' and 'hold off' commands are used to enable multiple plots on the same figure.

Note that you may need to adjust the size and values in the array to match your specific data.

This code will generate a surface plot with the peaks plotted as red dots.

Hope this helps!

gistlibby LogSnag