<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088</link>
    <title>MATLAB Central Newsreader - Find Minimum Values of Matrix without MIN or For loops</title>
    <description>Feed for thread: Find Minimum Values of Matrix without MIN or For loops</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 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.es/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 01 May 2011 02:39:04 +0000</pubDate>
      <title>Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833852</link>
      <author>Stephen Smitherman</author>
      <description>I am reviewing for a test and my teacher loves to ask us questions that require the removal of explicit loops (FOR and WHILE) and use implicit (Array Operations) instead. For example instead of &lt;br&gt;
&lt;br&gt;
for n=1:100&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b=sin(n);&lt;br&gt;
end&lt;br&gt;
b&lt;br&gt;
&lt;br&gt;
instead use &lt;br&gt;
&lt;br&gt;
n=1:100;&lt;br&gt;
b=sin(n);&lt;br&gt;
b&lt;br&gt;
&lt;br&gt;
so the function we are to convert is as follows&lt;br&gt;
A is an arbitrary predifined Matrix&lt;br&gt;
&lt;br&gt;
clear,clc&lt;br&gt;
for m=1:length(A(1,:));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;S(m)=A(1,m);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k=2:length(A(:,1));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(S(m)&amp;gt;A(k,m))&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;S(m)=A(k,m);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
S&lt;br&gt;
&lt;br&gt;
Where S is a vector with the minimum value of each column.&lt;br&gt;
I know this could be solved with Min(A), but that is not what is being ask. How would I use vectorization to make the same above operation to work?</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 03:24:04 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833854</link>
      <author>Roger Stafford</author>
      <description>&amp;nbsp;&amp;nbsp;I don't think you can hope to eliminate both for-loops within the spirit of your assignment.  If you replace the "if-end" construct with the appropriate logical vectors, however, you could accomplish the task with a single for-loop.  End of hint.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 07:46:04 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833862</link>
      <author>m4 Chrennikov</author>
      <description>If I get it clearly, you are searching the minimum element in each column?&lt;br&gt;
min(A)&lt;br&gt;
can replace all code.</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 07:54:05 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833863</link>
      <author>m4 Chrennikov</author>
      <description>oops :-)&lt;br&gt;
&lt;br&gt;
here is another code&lt;br&gt;
&lt;br&gt;
clear,clc&lt;br&gt;
sz = [5 8];&lt;br&gt;
A = rand(sz);&lt;br&gt;
S = sort(A);&lt;br&gt;
S(1,:)</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 08:02:20 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833865</link>
      <author>Nasser M. Abbasi</author>
      <description>On 5/1/2011 12:46 AM, m4 Chrennikov wrote:&lt;br&gt;
&amp;gt; If I get it clearly, you are searching the minimum element in each column?&lt;br&gt;
&amp;gt; min(A)&lt;br&gt;
&amp;gt; can replace all code.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
but OP said clearly they do _not_ want to use MIN.&lt;br&gt;
&lt;br&gt;
It is even in the subject.</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 18:34:44 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833914</link>
      <author>Srikanth</author>
      <description>For the minimum of the entire matrix, do V=sort(A(:)). Then V(1)&lt;br&gt;
contains the smallest value of the entire matrix.</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 19:09:04 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833917</link>
      <author>Roger Stafford</author>
      <description>"m4 Chrennikov" &amp;lt;abrek77@gmail.com&amp;gt; wrote in message &amp;lt;ipj3it$or2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; .......&lt;br&gt;
&amp;gt; S = sort(A);&lt;br&gt;
&amp;gt; S(1,:)&lt;br&gt;
Srikanth &amp;lt;skt@xdtech.com&amp;gt; wrote in message &amp;lt;d46cdd28-70e5-4816-95bd-fa7afe0cbe56@r27g2000prr.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; For the minimum of the entire matrix, do V=sort(A(:)). Then V(1)&lt;br&gt;
&amp;gt; contains the smallest value of the entire matrix.&lt;br&gt;
- - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;Replacing an order O(m*n) algorithm with an O(m*log(m)*n) or an O(m*n*log(m*n)) algorithm (for A an m x n matrix) involving sorting is hardly likely to be appreciated by Stephen's teacher in my opinion.  Finding the vector minimum in that problem is an inherently order O(m*n) problem and he would do well to restrict himself to solutions of that order of complexity.  The number of lines of code used is surely not a measure of the quality of a solution.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 19:39:05 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833922</link>
      <author>John D'Errico</author>
      <description>"Roger Stafford" wrote in message &amp;lt;ipkb4g$9uf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "m4 Chrennikov" &amp;lt;abrek77@gmail.com&amp;gt; wrote in message &amp;lt;ipj3it$or2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; .......&lt;br&gt;
&amp;gt; &amp;gt; S = sort(A);&lt;br&gt;
&amp;gt; &amp;gt; S(1,:)&lt;br&gt;
&amp;gt; Srikanth &amp;lt;skt@xdtech.com&amp;gt; wrote in message &amp;lt;d46cdd28-70e5-4816-95bd-fa7afe0cbe56@r27g2000prr.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; For the minimum of the entire matrix, do V=sort(A(:)). Then V(1)&lt;br&gt;
&amp;gt; &amp;gt; contains the smallest value of the entire matrix.&lt;br&gt;
&amp;gt; - - - - - - - - -&lt;br&gt;
&amp;gt;   Replacing an order O(m*n) algorithm with an O(m*log(m)*n) or an O(m*n*log(m*n)) algorithm (for A an m x n matrix) involving sorting is hardly likely to be appreciated by Stephen's teacher in my opinion.  Finding the vector minimum in that problem is an inherently order O(m*n) problem and he would do well to restrict himself to solutions of that order of complexity.  The number of lines of code used is surely not a measure of the quality of a solution.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
I'm not sure that is true. There was no statement of&lt;br&gt;
asymptotic efficiency as a requirement. We don't&lt;br&gt;
know what is the goal of the instructor is in this case.&lt;br&gt;
&lt;br&gt;
The stated goal was merely to find a solution that did&lt;br&gt;
not use min and did not employ an explicit loop. The&lt;br&gt;
application of sort satisfies all stated requirements.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 01 May 2011 21:57:04 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#833930</link>
      <author>Adrien Leygue</author>
      <description>Ok, here is a one-liner solution&lt;br&gt;
&lt;br&gt;
As other answers have pointed, it is difficult to provide an answer to your problem that would have a computational complexity equivalent to the simplest loop that would look for the min over all columns. Yet, this is one of the the point of vectorizing it is sometimes faster to compute too much stuff if an efficient way than just what you need.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; S = A((repmat(eye(size(A,1)),[1 size(A,1)]) *...&lt;br&gt;
(kron(A,ones(size(A,1),1)) &amp;lt; kron(ones(size(A,1),1),A)))==0)'&lt;br&gt;
&lt;br&gt;
basically:&lt;br&gt;
1-use kron to generate all pairwise comparisons between the elements.&lt;br&gt;
2-use a matrix matrix product to reduce all the comparisons and for each element of A, compute how many elements of the column are less than this element.&lt;br&gt;
3-the min over each columns is the element wich have zero in their corresponding entry.&lt;br&gt;
4-use logical indexing to extract the values&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
A.</description>
    </item>
    <item>
      <pubDate>Mon, 02 May 2011 23:35:20 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#834094</link>
      <author>Roger Stafford</author>
      <description>"Adrien Leygue" wrote in message &amp;lt;ipkkvg$5ti$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Ok, here is a one-liner solution&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; As other answers have pointed, it is difficult to provide an answer to your problem that would have a computational complexity equivalent to the simplest loop that would look for the min over all columns. Yet, this is one of the the point of vectorizing it is sometimes faster to compute too much stuff if an efficient way than just what you need.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; S = A((repmat(eye(size(A,1)),[1 size(A,1)]) *...&lt;br&gt;
&amp;gt; (kron(A,ones(size(A,1),1)) &amp;lt; kron(ones(size(A,1),1),A)))==0)'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; basically:&lt;br&gt;
&amp;gt; 1-use kron to generate all pairwise comparisons between the elements.&lt;br&gt;
&amp;gt; 2-use a matrix matrix product to reduce all the comparisons and for each element of A, compute how many elements of the column are less than this element.&lt;br&gt;
&amp;gt; 3-the min over each columns is the element wich have zero in their corresponding entry.&lt;br&gt;
&amp;gt; 4-use logical indexing to extract the values&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A.&lt;br&gt;
- - - - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;That is an ingenious one-line solution, Adrien.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I notice you can reduce it from complexity O(n^3*m) to only O(n^2*m) (where A is n by m) by doing a reshape over to a three-dimensional n x n x m size of the n^2 x m result of comparison between the two 'kron' products.  Then you can do a "not-any" operation on the columns of this 3D matrix which should give you the same result.  (Or if I have this wrong, something along these lines.)  Multiplication by the repeated 'eye' matrix which makes it O(n^3*m) is surely not necessary.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The result of this algorithm differs from that of 'min' in cases where there are repetitions of the minimum value within columns of A.  Each repetition would appear in the final S giving it more than m values, whereas 'min' would only give the first encountered in each column.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I still claim it might be a risky thing to hand in a solution to a teacher of greater complexity than that needed by 'min'.  I would fear their grade score would be awarded in inverse proportion to the complexity used.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 10 May 2011 21:35:15 +0000</pubDate>
      <title>Re: Find Minimum Values of Matrix without MIN or For loops</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/307088#835327</link>
      <author>Adrien Leygue</author>
      <description>&lt;br&gt;
&amp;gt;   I still claim it might be a risky thing to hand in a solution to a teacher of greater complexity than that needed by 'min'.  I would fear their grade score would be awarded in inverse proportion to the complexity used.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Well, if I were to ask to my students such an exercise (min over all columns) without loops and without the min function, I would not grade on efficiency. The problem itself is twisted. Imho, the only benefit from such exercise is to challenge the student creativity. In a sense the teacher is asking the students to surprise him.&lt;br&gt;
&lt;br&gt;
A.</description>
    </item>
  </channel>
</rss>
