<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588</link>
    <title>MATLAB Central Newsreader - Getting the min and max of a matrix</title>
    <description>Feed for thread: Getting the min and max of a matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 28 Feb 2009 18:55:04 -0500</pubDate>
      <title>Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631575</link>
      <author>Diego Zegarra</author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
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,&lt;br&gt;
&lt;br&gt;
A=&lt;br&gt;
[Inf 26 Inf 21 24;&lt;br&gt;
25 Inf Inf 24 22;&lt;br&gt;
23 27 Inf 22 25;&lt;br&gt;
28 20 Inf Inf 21;&lt;br&gt;
22 24 Inf 23 Inf]&lt;br&gt;
&lt;br&gt;
B = [1 4 3; 4 1 2]&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
I hope I made myself clear.&lt;br&gt;
&lt;br&gt;
I was finding the max and min without the constraint of matrix B the following way,&lt;br&gt;
&lt;br&gt;
Minimum = min(A(1,:));&lt;br&gt;
&lt;br&gt;
m = A(4,:)&lt;br&gt;
Maximum = max(max(h(isfinite(h)),1));&lt;br&gt;
&lt;br&gt;
Thank you so much!</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 19:14:02 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631578</link>
      <author>Matt Fig</author>
      <description>B = [1 4 3; 4 1 2]&lt;br&gt;
&lt;br&gt;
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?</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 20:13:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631587</link>
      <author>Diego Zegarra</author>
      <description>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.&lt;br&gt;
&lt;br&gt;
B = [1 4 3; 4 1 2]</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 20:34:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631589</link>
      <author>Roger Stafford</author>
      <description>&quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &amp;lt;goc5sd$bvd$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; B = [1 4 3; 4 1 2]&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 20:44:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631592</link>
      <author>Diego Zegarra</author>
      <description>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.</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 20:54:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631596</link>
      <author>us</author>
      <description>&quot;Diego Zegarra&quot;&lt;br&gt;
&amp;gt; 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,&lt;br&gt;
&amp;gt; A=&lt;br&gt;
&amp;gt; [Inf 26 Inf 21 24;&lt;br&gt;
&amp;gt; 25 Inf Inf 24 22;&lt;br&gt;
&amp;gt; 23 27 Inf 22 25;&lt;br&gt;
&amp;gt; 28 20 Inf Inf 21;&lt;br&gt;
&amp;gt; 22 24 Inf 23 Inf]&lt;br&gt;
&amp;gt; B = [1 4 3; 4 1 2]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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...&lt;br&gt;
&lt;br&gt;
one of the solutions&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a=[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf    26   Inf    21    24&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;25   Inf   Inf    24    22&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;23    27   Inf    22    25&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;28    20   Inf   Inf    21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22    24   Inf    23   Inf&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b=[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     4     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     1     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ix=sub2ind(size(a),b(:,1),b(:,2));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a(ix)=inf;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(a);&lt;br&gt;
%{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf    26   Inf    Inf    24&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;25   Inf   Inf    24    22&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;23    27   Inf    22    25&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf    20   Inf   Inf    21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22    24   Inf    23   Inf&lt;br&gt;
&lt;br&gt;
%}&lt;br&gt;
% now look for the min/max in the new A...&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Sat, 28 Feb 2009 20:54:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631597</link>
      <author>Roger Stafford</author>
      <description>&quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &amp;lt;goc7mh$8er$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Use a temporary copy of A with the altered values for taking the min's and max's!&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 03:11:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631624</link>
      <author>Diego Zegarra</author>
      <description>Is there any way to do this without using a temporary copy of A?</description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 06:24:02 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631635</link>
      <author>Matt Fig</author>
      <description>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. &lt;br&gt;
&lt;br&gt;
&amp;nbsp;a=[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inf 26 Inf 21 24&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;25 Inf Inf 24 22&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;23 27 Inf 22 25&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;28 20 Inf Inf 21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22 24 Inf 23 Inf&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;];&lt;br&gt;
b=[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1 4 3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4 1 2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;];&lt;br&gt;
&lt;br&gt;
&amp;nbsp;min(a(b(1,1),[1:b(1,2)-1, b(1,2)+1:end]))&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
If all of the infs are positive this should work whatever the rest of the values.  &lt;br&gt;
I don't see any way of finding the max without first dealing with the infs.  One approach is to copy the fourth row:&lt;br&gt;
&lt;br&gt;
ar = a(b(2,1),:).*isfinite(a(b(2,1),:))&lt;br&gt;
max(ar(1,[1:b(2,2)-1, b(2,2)+1:end]))</description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 08:13:02 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631646</link>
      <author>Bruno Luong</author>
      <description>&quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &amp;lt;gocuc5$mmf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Is there any way to do this without using a temporary copy of A?&lt;br&gt;
&lt;br&gt;
Any good reason why this is requires?&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 17:41:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631705</link>
      <author>Diego Zegarra</author>
      <description>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.&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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. </description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 17:57:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631710</link>
      <author>Matt Fig</author>
      <description>&quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; However if that is the only way it can be done then I guess I will just have to go that way. &lt;br&gt;
&lt;br&gt;
Did you read my post above?  </description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 18:07:01 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631713</link>
      <author>Bruno Luong</author>
      <description>&quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &amp;lt;goehbd$71n$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So making a copy would take too much space in this case.&lt;br&gt;
&lt;br&gt;
112 kbytes is  too much? No comment.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 01 Mar 2009 18:35:18 -0500</pubDate>
      <title>Re: Getting the min and max of a matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/245588#631718</link>
      <author>Jos </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;goeis5$f15$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Diego Zegarra&quot; &amp;lt;diegozbb@gmail.com&amp;gt; wrote in message &amp;lt;goehbd$71n$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So making a copy would take too much space in this case.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 112 kbytes is  too much? No comment.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Maybe the OP is programming on a very old machine .... I remember the days, when you could upgrade your ZX81 with 16K !! &lt;br&gt;
&lt;br&gt;
Jos</description>
    </item>
  </channel>
</rss>

