Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Is there any way to vectorize/ speed up calculation?
Date: Sat, 1 Dec 2012 05:15:13 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 37
Message-ID: <k9c3p1$kak$1@newscl01ah.mathworks.com>
References: <k9b1pc$cvq$1@newscl01ah.mathworks.com> <k9bk65$8uf$1@newscl01ah.mathworks.com> <k9bqq3$pst$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1354338913 20820 172.30.248.48 (1 Dec 2012 05:15:13 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 1 Dec 2012 05:15:13 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:783908

"Pete sherer" wrote in message <k9bqq3$pst$1@newscl01ah.mathworks.com>...
> nThres is actually 1-by-3 array.  It's the length( uniqCol1) that is large, about 500k. Each block has about 4-5 sub-blocks, so the total number of rows is about 2-2.5M rows. 
> 
> Since it has to run the loop, here 500k times it's just not fast at all. 
> 
> This line computes really fast: 
> flExc = repmat( tdata(:,3), 1, nThres) > repmat( thres, length(tdata(:,3)), 1);
> 
> That's why I want to find ways to avoid loops and do something like above if possible.
> 
> Thanks,
> Pete
- - - - - - - - -
  Well, that rules out the use of 'histc'.  However, there is all the more reason to get rid of the line

 tindx = (tdata(:,1)==uniqCol1( runi));

  Try this:

t = sortrows(tdata,[1,2]); % <-- Do this if tdata is not already sorted
v = t(:,3);
E = bsxfun(@gr,v,thres);
f = find([true;diff(t(:,1))>0;true]);
n = length(f)-1;
sumVal = zeros(n,1);
maxVal = zeros(n,1);
tcnt = zeros(n,size(E,2));
for k = 1:n
  r = f(k):f(k+1)-1;
  sumVal(k) = sum(v(r),1);
  maxVal(k) = max(v(r),[],1);
  tcnt(k,:) = sum(E(r,:),1);
end

  You can also consider using the function 'accumarray'.  It can presumably vectorize your whole processing but I don't know whether it would be faster or not.

Roger Stafford