http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588
MATLAB Central Newsreader  Getting the min and max of a matrix
Feed for thread: Getting the min and max of a matrix
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 28 Feb 2009 18:55:04 +0000
Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631575
Diego Zegarra
Hi all,<br>
<br>
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,<br>
<br>
A=<br>
[Inf 26 Inf 21 24;<br>
25 Inf Inf 24 22;<br>
23 27 Inf 22 25;<br>
28 20 Inf Inf 21;<br>
22 24 Inf 23 Inf]<br>
<br>
B = [1 4 3; 4 1 2]<br>
<br>
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.<br>
<br>
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.<br>
<br>
I hope I made myself clear.<br>
<br>
I was finding the max and min without the constraint of matrix B the following way,<br>
<br>
Minimum = min(A(1,:));<br>
<br>
m = A(4,:)<br>
Maximum = max(max(h(isfinite(h)),1));<br>
<br>
Thank you so much!

Sat, 28 Feb 2009 19:14:02 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631578
Matt Fig
B = [1 4 3; 4 1 2]<br>
<br>
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?

Sat, 28 Feb 2009 20:13:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631587
Diego Zegarra
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.<br>
<br>
B = [1 4 3; 4 1 2]

Sat, 28 Feb 2009 20:34:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631589
Roger Stafford
"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goc5sd$bvd$1@fred.mathworks.com>...<br>
> 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.<br>
> <br>
> B = [1 4 3; 4 1 2]<br>
<br>
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.<br>
<br>
Roger Stafford

Sat, 28 Feb 2009 20:44:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631592
Diego Zegarra
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.

Sat, 28 Feb 2009 20:54:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631596
us
"Diego Zegarra"<br>
> 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,<br>
> A=<br>
> [Inf 26 Inf 21 24;<br>
> 25 Inf Inf 24 22;<br>
> 23 27 Inf 22 25;<br>
> 28 20 Inf Inf 21;<br>
> 22 24 Inf 23 Inf]<br>
> B = [1 4 3; 4 1 2]<br>
> <br>
> 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...<br>
<br>
one of the solutions<br>
<br>
a=[<br>
Inf 26 Inf 21 24<br>
25 Inf Inf 24 22<br>
23 27 Inf 22 25<br>
28 20 Inf Inf 21<br>
22 24 Inf 23 Inf<br>
];<br>
b=[<br>
1 4 3<br>
4 1 2<br>
];<br>
ix=sub2ind(size(a),b(:,1),b(:,2));<br>
a(ix)=inf;<br>
disp(a);<br>
%{<br>
Inf 26 Inf Inf 24<br>
25 Inf Inf 24 22<br>
23 27 Inf 22 25<br>
Inf 20 Inf Inf 21<br>
22 24 Inf 23 Inf<br>
<br>
%}<br>
% now look for the min/max in the new A...<br>
<br>
us

Sat, 28 Feb 2009 20:54:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631597
Roger Stafford
"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goc7mh$8er$1@fred.mathworks.com>...<br>
> 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.<br>
<br>
Use a temporary copy of A with the altered values for taking the min's and max's!<br>
<br>
Roger Stafford

Sun, 01 Mar 2009 03:11:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631624
Diego Zegarra
Is there any way to do this without using a temporary copy of A?

Sun, 01 Mar 2009 06:24:02 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631635
Matt Fig
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. <br>
<br>
a=[<br>
Inf 26 Inf 21 24<br>
25 Inf Inf 24 22<br>
23 27 Inf 22 25<br>
28 20 Inf Inf 21<br>
22 24 Inf 23 Inf<br>
];<br>
b=[<br>
1 4 3<br>
4 1 2<br>
];<br>
<br>
min(a(b(1,1),[1:b(1,2)1, b(1,2)+1:end]))<br>
<br>
<br>
<br>
If all of the infs are positive this should work whatever the rest of the values. <br>
I don't see any way of finding the max without first dealing with the infs. One approach is to copy the fourth row:<br>
<br>
ar = a(b(2,1),:).*isfinite(a(b(2,1),:))<br>
max(ar(1,[1:b(2,2)1, b(2,2)+1:end]))

Sun, 01 Mar 2009 08:13:02 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631646
Bruno Luong
"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gocuc5$mmf$1@fred.mathworks.com>...<br>
> Is there any way to do this without using a temporary copy of A?<br>
<br>
Any good reason why this is requires?<br>
<br>
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.<br>
<br>
Bruno

Sun, 01 Mar 2009 17:41:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631705
Diego Zegarra
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.<br>
<br>
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.<br>
<br>
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.

Sun, 01 Mar 2009 17:57:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631710
Matt Fig
"Diego Zegarra" <diegozbb@gmail.com> wrote in message <br>
> However if that is the only way it can be done then I guess I will just have to go that way. <br>
<br>
Did you read my post above?

Sun, 01 Mar 2009 18:07:01 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631713
Bruno Luong
"Diego Zegarra" <diegozbb@gmail.com> wrote in message <goehbd$71n$1@fred.mathworks.com>...<br>
> <br>
> So making a copy would take too much space in this case.<br>
<br>
112 kbytes is too much? No comment.<br>
<br>
Bruno

Sun, 01 Mar 2009 18:35:18 +0000
Re: Getting the min and max of a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631718
Jos
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <goeis5$f15$1@fred.mathworks.com>...<br>
> "Diego Zegarra" <diegozbb@gmail.com> wrote in message <goehbd$71n$1@fred.mathworks.com>...<br>
> > <br>
> > So making a copy would take too much space in this case.<br>
> <br>
> 112 kbytes is too much? No comment.<br>
> <br>
> Bruno<br>
<br>
Maybe the OP is programming on a very old machine .... I remember the days, when you could upgrade your ZX81 with 16K !! <br>
<br>
Jos