Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
2 minimum numbers

Subject: 2 minimum numbers

From: Fahad Saleem

Date: 22 Dec, 2012 17:07:07

Message: 1 of 6

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
[(i-1, j-1) (i-1, j) (i-1, j+1);
 (i, j-1) (i, j) (i, j+1);
 (i+1, j-1) (i+1, j) (i+1, j+1)]
* minimum is at location (i,j)
and find again minimum and 2nd minimum number

Subject: 2 minimum numbers

From: dpb

Date: 22 Dec, 2012 17:45:44

Message: 2 of 6

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
> [(i-1, j-1) (i-1, j) (i-1, j+1);
> (i, j-1) (i, j) (i, j+1);
> (i+1, j-1) (i+1, j) (i+1, j+1)]
> * minimum is at location (i,j)
> and find again minimum and 2nd minimum number

Simple enough...multiple options, for such a small subset it's probably
simpler to just use sort() and select the first two elements rather than
do the min() and reduction.

To skip the center location simply replace the x(i,j)=NaN; for
i,j==desired location.

Base step (you'll want to generalize, of course)...

i=6; j=13;
xtarg=x(i-1:i+1,j-1:j+1);
xtarg(2,2)=nan;
[xmin,idx]=sort(xtarg(:));

Now you've got the info for the two min values (xmin(1:2) and the
locations in the idx vector from which to find the location for the new i,j)

Salt to suit...

--

Subject: 2 minimum numbers

From: Nasser M. Abbasi

Date: 22 Dec, 2012 18:12:45

Message: 3 of 6

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
> [(i-1, j-1) (i-1, j) (i-1, j+1);
> (i, j-1) (i, j) (i, j+1);
> (i+1, j-1) (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(row-1:row+1,col-1: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-(2-I);
J = col-(2-J);
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

Subject: 2 minimum numbers

From: Roger Stafford

Date: 22 Dec, 2012 18:37:08

Message: 4 of 6

"Fahad Saleem" wrote in message <kb4pbr$c14$1@newscl01ah.mathworks.com>...
> 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 :(
>.....
- - - - - - - - - - -
  If you intend this to be an iterative procedure, as Nasser warns, you will need to modify it to allow for running into an edge. Also you need to do something about getting stuck in an infinite repeating iteration - that is, traveling around in some repeating "minimum" circuit within your matrix.

 % Initial values
 n = 25;
 a = rand(n);
 ix = 6; jx = 13;
 r = [-1 0 1 1 1 0 -1 -1];
 c = [-1 -1 -1 0 1 1 1 0];

 % The iteration
 [~,k] = min(a(r+ix+n*(c-1+jx)));
 ix = ix + r(k); jx = jx + c(k);

Roger Stafford

Subject: 2 minimum numbers

From: Fahad Saleem

Date: 25 Dec, 2012 15:22:07

Message: 5 of 6

Bro I need more help how can I contact you?
"Nasser M. Abbasi" wrote in message <kb4t6u$eae$1@speranza.aioe.org>...
> 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
> > [(i-1, j-1) (i-1, j) (i-1, j+1);
> > (i, j-1) (i, j) (i, j+1);
> > (i+1, j-1) (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(row-1:row+1,col-1: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-(2-I);
> J = col-(2-J);
> 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

Subject: 2 minimum numbers

From: Nasser M. Abbasi

Date: 25 Dec, 2012 17:19:27

Message: 6 of 6

On 12/25/2012 9:22 AM, Fahad Saleem wrote:
> Bro I need more help how can I contact you?

Please continue discussion in this thread. This is a Usenet
public group, and many people can answer the same question and
participate in the discussion for the benefits of others.

If you have a follow up, simply continue in the same thread here
and ask. Make sure the question is clear so to get a better answer.

good luck,
--Nasser

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us