Help with optimising my code?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 11 de Feb. de 2018
1 voto
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 comentario
Rene Higuita
el 11 de Feb. de 2018
Editada: Rene Higuita
el 11 de Feb. de 2018
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!