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:
Getting the min and max of a matrix

Subject: Getting the min and max of a matrix

From: Diego Zegarra

Date: 28 Feb, 2009 18:55:04

Message: 1 of 14

Hi all,

My question here is how to find the maximum and minimum of a row in a matrix but with a small constraint. Assume we have a matrix,

A=
[Inf 26 Inf 21 24;
25 Inf Inf 24 22;
23 27 Inf 22 25;
28 20 Inf Inf 21;
22 24 Inf 23 Inf]

B = [1 4 3; 4 1 2]

Now I want to find the minimum of row 1 in matrix A. It would be 21, however I want to see matrix B first 2 columns and can see that first row has (1 4) and second row (4 1). Since 21 is in position (1,4) then that cannot be the minimum, so I have to find the minimum not including that one, so it would be 24 as it is not in matrix B.

Same thing for maximum, without counting the Inf. If I was to find the max finite number in row 4 of matrix A it is 28, however that is in position (4,1) and (4 1) is in matrix B so the max would actually be 21.

I hope I made myself clear.

I was finding the max and min without the constraint of matrix B the following way,

Minimum = min(A(1,:));

m = A(4,:)
Maximum = max(max(h(isfinite(h)),1));

Thank you so much!

Subject: Getting the min and max of a matrix

From: Matt Fig

Date: 28 Feb, 2009 19:14:02

Message: 2 of 14

B = [1 4 3; 4 1 2]

How do you decide what is in matrix B? From the first row, are positions (1,4) (4,3) (1,3) (3,4) (3,1) (4,1) not allowed? And similarly for the second row? Or is it only (1,4) and (4,3) that are not allowed?

Subject: Getting the min and max of a matrix

From: Diego Zegarra

Date: 28 Feb, 2009 20:13:01

Message: 3 of 14

As stated on my explanation you only see the first two columns, so first row means only position (1,4) is not allowed and second row only position (4,1) is not allowed. This matrix B will be changing dynamically every iteration. The thrid column means how many iterations that move is not allowed, every iteration it will decrease 1 unit and also the list will set another move to be in the list. Once the third column gets a value of 0, that row will be deleted from matrix B.

B = [1 4 3; 4 1 2]

Subject: Getting the min and max of a matrix

From: Roger Stafford

Date: 28 Feb, 2009 20:34:01

Message: 4 of 14

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goc5sd$bvd$1@fred.mathworks.com>...
> As stated on my explanation you only see the first two columns, so first row means only position (1,4) is not allowed and second row only position (4,1) is not allowed. This matrix B will be changing dynamically every iteration. The thrid column means how many iterations that move is not allowed, every iteration it will decrease 1 unit and also the list will set another move to be in the list. Once the third column gets a value of 0, that row will be deleted from matrix B.
>
> B = [1 4 3; 4 1 2]

  For each of the two column pair of indices in B I would replace the corresponding element in A by 'NaN', as well as all 'inf' values. Then you can do your normal 'max' and 'min' operations. These ignore all NaNs.

Roger Stafford

Subject: Getting the min and max of a matrix

From: Diego Zegarra

Date: 28 Feb, 2009 20:44:01

Message: 5 of 14

The thing is matrix A has to stay as it is since I need those values in there for later. I cannot just replace by NaN.

Subject: Getting the min and max of a matrix

From: us

Date: 28 Feb, 2009 20:54:01

Message: 6 of 14

"Diego Zegarra"
> My question here is how to find the maximum and minimum of a row in a matrix but with a small constraint. Assume we have a matrix,
> A=
> [Inf 26 Inf 21 24;
> 25 Inf Inf 24 22;
> 23 27 Inf 22 25;
> 28 20 Inf Inf 21;
> 22 24 Inf 23 Inf]
> B = [1 4 3; 4 1 2]
>
> Now I want to find the minimum of row 1 in matrix A. It would be 21, however I want to see matrix B first 2 columns and can see that first row has (1 4) and second row (4 1). Since 21 is in position (1,4) then that cannot be the minimum, so I have to find the minimum not including that one, so it would be 24 as it is not in matrix B...

one of the solutions

     a=[
          Inf 26 Inf 21 24
           25 Inf Inf 24 22
           23 27 Inf 22 25
           28 20 Inf Inf 21
           22 24 Inf 23 Inf
     ];
     b=[
          1 4 3
          4 1 2
     ];
     ix=sub2ind(size(a),b(:,1),b(:,2));
     a(ix)=inf;
     disp(a);
%{
          Inf 26 Inf Inf 24
           25 Inf Inf 24 22
           23 27 Inf 22 25
          Inf 20 Inf Inf 21
           22 24 Inf 23 Inf

%}
% now look for the min/max in the new A...

us

Subject: Getting the min and max of a matrix

From: Roger Stafford

Date: 28 Feb, 2009 20:54:01

Message: 7 of 14

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goc7mh$8er$1@fred.mathworks.com>...
> The thing is matrix A has to stay as it is since I need those values in there for later. I cannot just replace by NaN.

  Use a temporary copy of A with the altered values for taking the min's and max's!

Roger Stafford

Subject: Getting the min and max of a matrix

From: Diego Zegarra

Date: 1 Mar, 2009 03:11:01

Message: 8 of 14

Is there any way to do this without using a temporary copy of A?

Subject: Getting the min and max of a matrix

From: Matt Fig

Date: 1 Mar, 2009 06:24:02

Message: 9 of 14

You don't have to copy all the matrix a, just the rows of interest. Otherwise, this finds the minimum for a without the index in the first row of b. That is if you never have negative infs in your matrix a.

 a=[
          Inf 26 Inf 21 24
           25 Inf Inf 24 22
           23 27 Inf 22 25
           28 20 Inf Inf 21
           22 24 Inf 23 Inf
     ];
b=[
     1 4 3
      4 1 2
     ];

 min(a(b(1,1),[1:b(1,2)-1, b(1,2)+1:end]))



If all of the infs are positive this should work whatever the rest of the values.
I don't see any way of finding the max without first dealing with the infs. One approach is to copy the fourth row:

ar = a(b(2,1),:).*isfinite(a(b(2,1),:))
max(ar(1,[1:b(2,2)-1, b(2,2)+1:end]))

Subject: Getting the min and max of a matrix

From: Bruno Luong

Date: 1 Mar, 2009 08:13:02

Message: 10 of 14

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gocuc5$mmf$1@fred.mathworks.com>...
> Is there any way to do this without using a temporary copy of A?

Any good reason why this is requires?

If a copy of A takes too much place compares to the list of index (B). That means you could implement a logical mask in sparse matrix, which will be very similar structured to B but much more versatile to work with.

Bruno

Subject: Getting the min and max of a matrix

From: Diego Zegarra

Date: 1 Mar, 2009 17:41:01

Message: 11 of 14

The thing is that in some cases I will have to look at matrices of size 120x120 and there will be 12 of those (instead of just a matrix A). For example I will have to look first for the minimum and maximum out of the diagonal of those 12 matrices not including the ones in the matrix B.

Matrix B actually has four columns, the first two used as stated before, the third shows the machine or number of matrix A and the last will count how many more iterations that will be there.

So making a copy would take too much space in this case. However if that is the only way it can be done then I guess I will just have to go that way.

Subject: Getting the min and max of a matrix

From: Matt Fig

Date: 1 Mar, 2009 17:57:01

Message: 12 of 14

"Diego Zegarra" <diegozbb@gmail.com> wrote in message
> However if that is the only way it can be done then I guess I will just have to go that way.

Did you read my post above?

Subject: Getting the min and max of a matrix

From: Bruno Luong

Date: 1 Mar, 2009 18:07:01

Message: 13 of 14

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goehbd$71n$1@fred.mathworks.com>...
>
> So making a copy would take too much space in this case.

112 kbytes is too much? No comment.

Bruno

Subject: Getting the min and max of a matrix

From: Jos

Date: 1 Mar, 2009 18:35:18

Message: 14 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <goeis5$f15$1@fred.mathworks.com>...
> "Diego Zegarra" <diegozbb@gmail.com> wrote in message <goehbd$71n$1@fred.mathworks.com>...
> >
> > So making a copy would take too much space in this case.
>
> 112 kbytes is too much? No comment.
>
> Bruno

Maybe the OP is programming on a very old machine .... I remember the days, when you could upgrade your ZX81 with 16K !!

Jos

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