Re: vectorize
- From: ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford)
- Date: Thu, 14 Sep 2006 11:25:08 GMT
In article
<ellieandrogerxyzzy-1309061958540001@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford) wrote:
In article <ef40a9e.-1@xxxxxxxxxxxxxxxxxxxxxxx>, jim <no@xxxxxxxxxx> wrote:----------------------
Hello,----------------------------------
Can anyone help vecotrize the following code please as the loop takes
extremely long to perform.
e = [1 1 1 2 2 2 2 2 3 3 3 3 3 ....etc.... ]
max(e) typically is around 50e3
a = [1 0 0 0 1 0 1 0 0 0 1 0 0....etc]
basically, I would like to find where the "1" occurs in a with repect
to the initial values of 1 ,2 ,3 etc... in e.
for i = 1:max(e)
p = find(e == i);
pp = a(p);
tt = find(pp >0);
s(1:length(tt),i) = tt;
end
answer in this case is
s = [1 2 3; 0 4 0];
thanks very much.
jim
I have some questions. I presume from your for-loop code that all
elements of e are positive integers. The questions are: 1) Are you
assuming that e is already in sorted order, as in your example? 2) Are
any integers missing in e between 1 and max(e)? 3) If so or if there are
some with no matching 1's in a, do you want the corresponding columns to
be present in s and consist of all zeros, (as would occur with your
for-loop?)
I believe your code is slow because for each pass through the loop, you
have to do a scan of the entire length of e with 'find(e==i)'. Also the
fact that s has not been alloted before entering the loop causes a
slowdown as it is augmented step-by-step throughout the loop.
Depending on your answers to the above questions, there is a vectorized
way of determining the maximum length of columns needed in s, and just
possibly a way of vectorizing the entire process. I do wonder, though,
why you aren't making s a cell array which would permit variable length
columns, rather than having the vacant spaces filled in with zeros.
Roger Stafford
Jim, I see Jos already has a vectorized solution, but since I made a
sort of promise to provide one, I'll give you mine anyway, (even though
Jos' looks shorter.) This code assumes only that the elements of e are
positive integers and all like numbers are grouped together, but not
necessarily in order of their size. Also a must have a like number of
elements and consist entirely of 1's and 0's. The code assumes that you
want to have all-zero columns for the integers which are missing between 1
and max(e) in e and also those for which there are no corresponding 1's in
a.
There are a lot of passes through e and a in this code with finds,
diffs, cumsums and the like, but with max(e) around 50000, I feel sure it
will be faster than the for-loop you've been using.
e = [1 1 1 2 2 2 2 2 3 3 3 3 3];
a = [1 0 0 0 1 0 1 0 0 0 1 0 0];
n = length(e);
p = find(a);
q = find([1,diff(e),1]);
ca = cumsum(a);
m = [0,ca];
m = diff(m(q));
mn = length(m);
i = zeros(1,n);
i(q(2:mn)) = m(1:mn-1);
i = ca-cumsum(i);
i = i(p);
j = e(p);
c = zeros(1,n);
c(q(2:mn)) = diff(q(1:mn));
c = (1:n) - cumsum(c);
c = c(p);
mx = max(m);
me = max(e);
s = zeros(1,me*mx);
s(i+mx*(j-1)) = c;
s = reshape(s,mx,me);
Roger Stafford
.
- Follow-Ups:
- Re: vectorize
- From: Jim
- Re: vectorize
- References:
- vectorize
- From: jim
- Re: vectorize
- From: Roger Stafford
- vectorize
- Prev by Date: Re: ignoring decimal points
- Next by Date: Re: ignoring decimal points
- Previous by thread: Re: vectorize
- Next by thread: Re: vectorize
- Index(es):
Relevant Pages
|