<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724</link>
    <title>MATLAB Central Newsreader - Find function</title>
    <description>Feed for thread: Find function</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, 22 Nov 2008 07:24:04 -0500</pubDate>
      <title>Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612545</link>
      <author>Gordon </author>
      <description>I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&lt;br&gt;
y=zeros(length(a),1);&lt;br&gt;
for i = 1:length(a)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;a1 = a(i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&lt;br&gt;
This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&lt;br&gt;
Any help would be appreciated.</description>
    </item>
    <item>
      <pubDate>Sat, 22 Nov 2008 08:33:04 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612547</link>
      <author>Johan Carlson</author>
      <description>&quot;Gordon &quot; &amp;lt;mloomis@gmail.com&amp;gt; wrote in message &amp;lt;gg8c2k$s8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y=zeros(length(a),1);&lt;br&gt;
&amp;gt; for i = 1:length(a)&lt;br&gt;
&amp;gt;    a1 = a(i);&lt;br&gt;
&amp;gt;    y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
&amp;gt; b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
&amp;gt; y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any help would be appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Try this:&lt;br&gt;
&lt;br&gt;
A = reshape(repmat(a',length(b),1),length(a)*length(b),1);&lt;br&gt;
B = repmat(b,length(a),1);&lt;br&gt;
y = sum(reshape(B&amp;lt;=A,length(b),length(a)));&lt;br&gt;
&lt;br&gt;
/JC</description>
    </item>
    <item>
      <pubDate>Sat, 22 Nov 2008 09:24:02 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612548</link>
      <author>Roger Stafford</author>
      <description>&quot;Gordon &quot; &amp;lt;mloomis@gmail.com&amp;gt; wrote in message &amp;lt;gg8c2k$s8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y=zeros(length(a),1);&lt;br&gt;
&amp;gt; for i = 1:length(a)&lt;br&gt;
&amp;gt;    a1 = a(i);&lt;br&gt;
&amp;gt;    y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
&amp;gt; b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
&amp;gt; y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any help would be appreciated.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Gordon, you have described your problem one way but have computed quite a different quantity.  For each element of a you have counted the number of elements in b that it is less than or equal to.  This is very different from &quot;the number of occurrences in vector a that are &amp;lt;= every occurrence in vector b&quot;.  The one is a vector as long as a is and the other is a single scalar number.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;To do what your for-loop does, try this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[p,p] = sort([a,b]);&lt;br&gt;
&amp;nbsp;t = length(b)-cumsum(p&amp;gt;length(a));&lt;br&gt;
&amp;nbsp;q = 1:length(p); q(p) = q;&lt;br&gt;
&amp;nbsp;y = t(q(1:length(a)));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;To do what you described in words would only require taking the 'min' of b and counting how many a's are less than equal to that minimum.  I doubt if that is what you meant.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 22 Nov 2008 09:32:01 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612549</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gg8j3i$3ih$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&amp;gt;  [p,p] = sort([a,b]);&lt;br&gt;
&amp;gt;  t = length(b)-cumsum(p&amp;gt;length(a));&lt;br&gt;
&amp;gt;  q = 1:length(p); q(p) = q;&lt;br&gt;
&amp;gt;  y = t(q(1:length(a)));&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&lt;br&gt;
Note: The way I wrote that assumed that a and b are row vectors.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 22 Nov 2008 09:35:02 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612550</link>
      <author>Bruno Luong</author>
      <description>&quot;Gordon &quot; &amp;lt;mloomis@gmail.com&amp;gt; wrote in message &amp;lt;gg8c2k$s8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y=zeros(length(a),1);&lt;br&gt;
&amp;gt; for i = 1:length(a)&lt;br&gt;
&amp;gt;    a1 = a(i);&lt;br&gt;
&amp;gt;    y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
&amp;gt; b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
&amp;gt; y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any help would be appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
This do not require to duplicate in 2D and takes less RAM.&lt;br&gt;
&amp;nbsp;&lt;br&gt;
[ua trast I]=unique(a);&lt;br&gt;
n=cumsum(histc(b,ua));&lt;br&gt;
y=n(I)&lt;br&gt;
&lt;br&gt;
You need to check about the correctness with &quot;&amp;lt;=&quot; and &quot;&amp;lt;&quot; of the code. If it does not meet the comparison requirement, work on reverse-sign arrays.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 23 Nov 2008 00:50:17 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#612639</link>
      <author>Roger Stafford</author>
      <description>&quot;Gordon &quot; &amp;lt;mloomis@gmail.com&amp;gt; wrote in message &amp;lt;gg8c2k$s8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y=zeros(length(a),1);&lt;br&gt;
&amp;gt; for i = 1:length(a)&lt;br&gt;
&amp;gt;    a1 = a(i);&lt;br&gt;
&amp;gt;    y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
&amp;gt; b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
&amp;gt; y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any help would be appreciated.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I realized belatedly that the code I sent you has its inequality testing backwards with respect to the for-loop code you gave.  Here is the corrected version.  It now assumes that a and b are column vectors.  For large arrays it should be considerably faster than your for-loop method.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;[p,p] = sort([b;a]);&lt;br&gt;
&amp;nbsp;t = cumsum(p&amp;lt;=length(b));&lt;br&gt;
&amp;nbsp;q = (1:length(p)); q(p) = q;&lt;br&gt;
&amp;nbsp;y = t(q(length(b)+1:end));&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Wed, 26 Nov 2008 00:23:01 -0500</pubDate>
      <title>Re: Find function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239724#613285</link>
      <author>Gordon </author>
      <description>Roger, &lt;br&gt;
Thanks for the help, this solution worked great!  Thanks to everyone else who replyed.&lt;br&gt;
ML&lt;br&gt;
&lt;br&gt;
&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gga9c9$l1c$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Gordon &quot; &amp;lt;mloomis@gmail.com&amp;gt; wrote in message &amp;lt;gg8c2k$s8d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I&amp;#8217;m attempting to run a find command on two vectors A and B, I want to find the number of occurrences in vector a  that are &amp;lt;= every occurrence in vector b.  I can do this easily with a for loop, but it takes forever to run on large vectors.  Here is my code&amp;#8230;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; y=zeros(length(a),1);&lt;br&gt;
&amp;gt; &amp;gt; for i = 1:length(a)&lt;br&gt;
&amp;gt; &amp;gt;    a1 = a(i);&lt;br&gt;
&amp;gt; &amp;gt;    y(i) = length(find(b &amp;lt;= a1));&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a= [1;1;2;3;4;5;7;9;3]&lt;br&gt;
&amp;gt; &amp;gt; b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]&lt;br&gt;
&amp;gt; &amp;gt; y= [4;4;7;12;15;15;15;15;12]&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; This works fine with small vectors, but I want to do this with very large vectors.  Is there any way to do this in Matlab without using the For Loop?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Any help would be appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   I realized belatedly that the code I sent you has its inequality testing backwards with respect to the for-loop code you gave.  Here is the corrected version.  It now assumes that a and b are column vectors.  For large arrays it should be considerably faster than your for-loop method.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  [p,p] = sort([b;a]);&lt;br&gt;
&amp;gt;  t = cumsum(p&amp;lt;=length(b));&lt;br&gt;
&amp;gt;  q = (1:length(p)); q(p) = q;&lt;br&gt;
&amp;gt;  y = t(q(length(b)+1:end));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; </description>
    </item>
  </channel>
</rss>

