find and plot maxima in 2d array in matlab in matlab

To find and plot the maxima in a 2D array in MATLAB, you can use the findpeaks function in combination with the plot function. Here's an example code:

main.m
% Example 2D array
array = [1, 2, 3, 4, 5;
         6, 7, 8, 9, 10;
         11, 12, 13, 14, 15;
         10, 9, 8, 7, 6;
         5, 4, 3, 2, 1];

% Find the peak values and their indices
[peaks, indices] = findpeaks(array(:));

% Plot the original array
figure;
subplot(1, 2, 1);
imagesc(array);
title('Original Array');

% Plot the maxima
subplot(1, 2, 2);
imagesc(array);
hold on;
plot(indices, peaks, 'r*', 'MarkerSize', 10);
title('Maxima');
448 chars
23 lines

In this example, we create a sample 2D array called array. We then use the findpeaks function to find the peak values (peaks) and their corresponding indices (indices) in the array. Finally, we plot the original array and overlay the maxima points on top using the plot function.

Note that the imagesc function is used to plot the 2D array in both subplots. You can modify the code according to your specific array dimensions and requirements.

related categories

gistlibby LogSnag