Help with optimising my code?
1 view (last 30 days)
Show older comments
Hello everyone,
Apologies for the vague title, but I think I'm missing exactly the knowledge that would let me point at the topic...
I am trying to group the values in a large matrix yMat into a single vector yOut, sorting them by binning values from a secondary matrix xMat. Starting with
[N,bin] = histc(xMat,edges); *
Then
for k=1:numel(edges)-1
yOut(k)=mean(yMat(bin==k));
end
xMat/yMat vary in size, but their numel can go up to a couple tens of millions, and I need to go through hundreds of them. I picked a relatively large example: the histc line takes 0.3s, while the loop takes over 4s when numel(edges)=100. Ideally I would like much finer grain, too, with numel(edges) closer to 1000...
I am pretty confident I do this the very wrong way, for loops never being a good sign, but I can't think of a faster way to do this. Any help appreciated!
*I know histc is a legacy function, but I'm stuck with R2011b right now, and i don't think this impacts the issue
0 Comments
Accepted Answer
per isakson
on 11 Feb 2018
Edited: per isakson
on 11 Feb 2018
xMat = randn( 12, 1);
edges = ( - 4 : 1 : 4 );
yMat = ( 1 : 12 )';
[ N, bin ] = histc( xMat, edges );
y1 = accumarray( bin, yMat, [], @mean );
for k=1:numel(edges)-1
y2(k)=mean(yMat(bin==k));
end
comparision
>> y1'
ans =
0 11.0000 8.5000 6.6667 4.0000 7.0000
>> y2
y2 =
NaN 11.0000 8.5000 6.6667 4.0000 7.0000 NaN NaN
btw: y2 should have been preallocated
In my example, elements of bin can get the value zero. Thus, bin+1 in accumarray
However, the increase in speed on R2016a is definitely not impressive.
4 Comments
per isakson
on 12 Feb 2018
Edited: per isakson
on 12 Feb 2018
Back in 2009 Yair Altman described a similar case, in which function handles cause a significant performance penalty, cellfun – undocumented performance boost. This has not changed in recent Matlab releases.
More Answers (1)
Walter Roberson
on 11 Feb 2018
Try accumarray. Use the bin number as subs, use ymat in the values slot, use the empty array as the size, use @mean as the function
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!