Use nested for loops to compare neighboring values within a matrix

2 views (last 30 days)
I have a homework assignment that wants to know the peaks and valleys within a matrix by comparing the maximums and minimums within an area. Our assignment wants all the peaks and valleys listed within 8 neighbors (in all four directions) of the matrix. I can understand finding the absolute max/mins, however I'm stumped on how to compare them to their neighbors within the matrix. Here's what I have so far: (elevation_data is our given matrix to operate with)
E = elevation_data;
min = Inf; max = -Inf;
NR = size(E,1);
for j = 2:(NR-1)
NC = size(E,2);
for k = 2:(NC-1)
if E(j,k) >= max
max = E(j,k);
maxc = max(j,k);
end
if E(j,k) <= min
min = E(j,k);
minc = [j,k];
end
end
end
How can I expand on this code to compare to its neighbors? (This is a sort-of introductory class, and I have to use nested for loops. I also cannot use "complex" functions as I either won't understand it or won't be able to use it without justifying its use) Thanks!

Answers (1)

Davide Masiello
Davide Masiello on 14 Apr 2022
Edited: Davide Masiello on 14 Apr 2022
Hi Brayden, I am going to give you an example of how this could be done.
In order to do so, let me create a matrix z that contains peaks and valleys.
[x,y] = meshgrid(linspace(-3,3,100));
z = peaks(linspace(-3,3,100));
surf(x,y,z)
Here, z is 100x100.
Now, let's code a nested loop that goes through all the elements and finds out if they are either a peak or a valley.
peak_row = [];
peak_col = [];
valley_row = [];
valley_col = [];
for row = 2:size(z,1)-1
for col = 2:size(z,2)-1
local_mat = z(row-1:row+1,col-1:col+1);
local_mat = local_mat(:);
local_mat(5) = [];
if all(z(row,col) > local_mat)
peak_row = [peak_row,row];
peak_col = [peak_col,col];
elseif all(z(row,col) < local_mat)
valley_row = [valley_row,row];
valley_col = [valley_col,col];
else
end
end
end
Let us check how many valleys and peaks it found
fprintf('There are %d valley and %d peaks',[length(valley_row),length(peak_row)])
There are 3 valley and 3 peaks
Which makes sense if you carefully look at the plot above.
Let us now, for instance, plot the peaks
surf(x,y,z)
hold on
plot3(x(peak_row(1),peak_col(1)),y(peak_row(1),peak_col(1)),z(peak_row(1),peak_col(1)),'*r')
plot3(x(peak_row(2),peak_col(2)),y(peak_row(2),peak_col(2)),z(peak_row(2),peak_col(2)),'*r')
plot3(x(peak_row(3),peak_col(3)),y(peak_row(3),peak_col(3)),z(peak_row(3),peak_col(3)),'*r')
And the same goes with the valleys.
Of course, this code ignores possible peaks and or valleys that you might find at the edge of the matrix.
You could definitely expand from here and implement that too.
As a last remark, there are functions in matlab that can do that with just a few lines of code (see for instance islocalmax).

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!