On 12/22/2012 11:07 AM, Fahad Saleem wrote:
> Hi I need some help. I have a 25x25 matrix of rand value between 2 & 2.5. I want to
>do following steps but I am stuck :(
>
> 1 finding minimum nd 2nd minimum number (not row or col just 1 element)
>in selected 3x3 matrix which is located at
> [(5,12) (5,13) (5,14);
> (6,12) (6,13) (6,14);
> (7,12) (7,13) (7,14)] of above matrix. but It must ignore (6,13) and find
>number in outer 8 locations.
>
> 2 from above minimum number it automatically select next matrix of 3x3 on following location
> [(i1, j1) (i1, j) (i1, j+1);
> (i, j1) (i, j) (i, j+1);
> (i+1, j1) (i+1, j) (i+1, j+1)]
> * minimum is at location (i,j)
> and find again minimum and 2nd minimum number
>
easy to do if you do not have to worry about running out of the edge of
the matrix. Because if you find the min at the edge, then in the next
step, you'll have to take a 3x3 matrix which now goes over the edge.
I'll let you handle these conditions by adding if statements,
right now it throws an exception.

function driver()
M=6; A=rand(M);
row=2; col=2; %starting index location
[I,J,B]=getAt(row,col,A);
[I,J,B]=getAt(I,J,A);
[I,J,B]=getAt(I,J,A);
%etc..
end
function [I,J,B]=getAt(row,col,A)
try
B=A(row1:row+1,col1:col+1);
catch ME
fprintf('opps, fill of the edge! not supported\n');
throw(ME)
end
tmp = B(2,2);
B(2,2) = inf;
[I,J] = find(B==min(min(B)));
B(2,2) = tmp;
I = row(2I);
J = col(2J);
end

on some test I get (starting at location 2,2 in A)
A =
0.6736 0.5985 0.4664 0.1758 0.5631 0.5181
0.9130 0.7314 0.4564 0.2949 0.3046 0.7892
0.7561 0.8708 0.1278 0.0587 0.7545 0.5712
0.1685 0.3334 0.6399 0.0602 0.6711 0.7665
0.9640 0.4630 0.8965 0.0267 0.9398 0.9202
0.3483 0.1669 0.4274 0.5505 0.4852 0.2103
B =
0.6736 0.5985 0.4664
0.9130 0.7314 0.4564
0.7561 0.8708 0.1278
B =
0.7314 0.4564 0.2949
0.8708 0.1278 0.0587
0.3334 0.6399 0.0602
B =
0.4564 0.2949 0.3046
0.1278 0.0587 0.7545
0.6399 0.0602 0.6711
Nasser
