From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find function
Date: Sat, 22 Nov 2008 09:24:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 30
Message-ID: <gg8j3i$3ih$>
References: <gg8c2k$s8d$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1227345842 3665 (22 Nov 2008 09:24:02 GMT)
NNTP-Posting-Date: Sat, 22 Nov 2008 09:24:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:502473

"Gordon " <> wrote in message <gg8c2k$s8d$>...
> I&#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 <= 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&#8230;
> y=zeros(length(a),1);
> for i = 1:length(a)
>    a1 = a(i);
>    y(i) = length(find(b <= a1));
> end
> a= [1;1;2;3;4;5;7;9;3]
> b=[ 1;2;3;4;1;2;3;3;3;4;1;1;2;3;4]
> y= [4;4;7;12;15;15;15;15;12]
> 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?
> Any help would be appreciated.

  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 "the number of occurrences in vector a that are <= every occurrence in vector b".  The one is a vector as long as a is and the other is a single scalar number.

  To do what your for-loop does, try this:

 [p,p] = sort([a,b]);
 t = length(b)-cumsum(p>length(a));
 q = 1:length(p); q(p) = q;
 y = t(q(1:length(a)));

  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.

Roger Stafford