Minimum cell of a matrix

I am trying to find the minimum-value cell of a matrix. To simplify, I take a 3X3 matrix, I tried this but no luck.
Row1=2;
Col1=2;%This is the mid-cell of the 3X3 Matrix
min=99;
if B(Row1,Col1)~=0
for Row =-1:1:1
for Col==-1:1:1 % this gets the adjacent cells of the 3X3 matrix
if B(Row+Row1, Col+Col1)<min
min=B(Row+Row1,Col+Col1);

Answers (1)

Marc Jakobi
Marc Jakobi on 11 Oct 2016
Edited: Marc Jakobi on 11 Oct 2016
You need to end every loop and if statement with an end keyword
Min=inf;
for Row =1:size(B,1)
for Col==1:size(B,2)
if B(Row, Col)<Min
Min=B(Row,Col);
end
end
end
Is this a homework assignment? If not, you can also just use
m = Min(B(:));

10 Comments

Thanks.
Tried it on this 5X5 natrix, starting with cell[2,2]:
B=[21,2,3,4,8;3,4,5,7,5;6,7,8,14,15;3,4,5,7,5;3,4,19,0,5];
SearchStart=[2 2];
I build a 3X3 matrix around 2,2 i.e. [1,2], [1,2], [1,3], [2,1], [2,2], [2,3] etc.and find the minimum of all the 9 elements. I then take
the row,cell of this minimum and add it to 2,2 to get the
location of the new minimum cell. I then repeat this for the new
cell i.e. form a 3X3 around it and again find the minimum. I put
these locations (row,col) in an Nx2 array. The object is to find the path with row, col of each minimum.
Any help appreciated.
You are making it a lot more complicated than you need to. The code I posted works on any size.
Ken
Ken on 12 Oct 2016
Edited: Ken on 12 Oct 2016
Thanks Marc. The reason is that I am trying to develop a path for a robot using a grid of 9X11. I am doing it by using a 3X3 matrix around each min cell of the 9X11 matrix - that way I go around a -ve gradient. The code you gave works good - only problem I don't know how to put the min coordinates in a nX2 matrix which I call OptimumPath. Appreciate any help.
To get the coordinates:
Min=inf;
for Row =1:size(B,1)
for Col==1:size(B,2)
if B(Row, Col)<Min
rI = Row; %row index
cI = Col; %column index
Min=B(Row,Col);
end
end
end
coordinates = [rI, cI];
or:
Min = min(B(:));
[rI, cI] = find(B == Min, 1);
coordinates = [rI, cI];
Ken
Ken on 12 Oct 2016
Edited: Walter Roberson on 12 Oct 2016
Thanks Marc. Tried it and it works.
I have to do this for an assignment. They insist that I do it in a 3X3 Matrix around each minimumpoint in the main matrix B. I HAVE TRIED THIS - NO LUCK. Have been at it for a month - Thanks Min=SearchStart(2,2);
%while SearchStart(Row,Col)~=0
Min=inf;
m=[];
%while and(Row+Row1<5 & Col+Col1<5)
for Row1 =-1:1:1 %x offset
for Col1=-1:1:1 %y offset
if B(Row+Row1, Col+Col1)<Min
Min=B(Row+Row1, Col+Col1);
x=Row1;
y=Col1;
SearchStart=[SearchStart(1)+Row1,SearchStart(2) +Col1];
%m=[x,y];
m=[m;x,y];
end
end
%m=[m;Row,Col]
end
%end
m
Use blockproc() with a window of 1 and a margin of [1 1] so that a 3 x 3 matrix will be received by the calculation function. Watch out for the behavior at the edges though; blockproc() offers several different approaches for the edges.
Ken
Ken on 12 Oct 2016
Thanks, I am a newbie to MATLAB. If possible an example would help.
blockproc(YourMatrix, [1 1], @(block) min(block.data(:)), 'BorderSize', [1 1])
Ken
Ken on 15 Oct 2016
Thanks. My matrix is the subject matrix. block.data is my 3X3 matrix?
Yes, block.data would be the 3 x 3 matrix currently being examined.

Sign in to comment.

Categories

Asked:

Ken
on 11 Oct 2016

Commented:

on 16 Oct 2016

Community Treasure Hunt

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

Start Hunting!