Help with optimising my code?
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
Accepted Answer
More Answers (1)
Walter Roberson
on 11 Feb 2018
1 vote
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
1 Comment
Rene Higuita
on 11 Feb 2018
Edited: Rene Higuita
on 11 Feb 2018
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!