find peaks in 2d array in matlab

To find peaks in a 2D array in MATLAB, you can use the imregionalmax function. This function identifies the local maxima (peaks) in an array by comparing each pixel with its surrounding neighbors.

Here's an example of how you can use the imregionalmax function:

main.m
% Create a sample 2D array
array = [1 1 1 1 1;
         1 2 3 4 1;
         1 3 9 3 1;
         1 4 3 2 1;
         1 1 1 1 1];

% Find the peaks in the array
peaks = imregionalmax(array);

% Display the original array and the peaks
figure;
subplot(1,2,1);
imshow(array);
title('Original Array');
subplot(1,2,2);
imshow(peaks);
title('Peaks');

% Get the indices of the peaks
[rows, columns] = find(peaks);
disp('Peak indices:');
disp([rows, columns]);
453 chars
24 lines

In this example, we create a sample 2D array array and then use the imregionalmax function to find the peaks in the array. The resulting peaks will be marked as 1 in the output binary array. We can then display the original array and the peaks using the imshow function.

To get the indices of the peaks, you can use the find function. The find function returns the row and column indices of the nonzero elements in the peaks array.

This code will output the indices of the peaks in the command window.

Note that the imregionalmax function considers local maxima, so it may not find the global maxima if there are multiple peaks with the same value. If you want to find the global maxima, you can use the max function to find the maximum value in the array and then find its indices using the find function.

related categories

gistlibby LogSnag