MATLAB Answers


How to vectorize this for maximum efficiency?

Asked by tensorisation on 31 Aug 2019
Latest activity Commented on by Guillaume
on 1 Sep 2019
In my calculation I had to go over non-zero elements of a 3 dimensional array (label). Instead of making 3 loops over each index of the array (which would be inefficient), I ended up doing something like this:
for q=1:length(label_lin)
Is there a way to get rid of this loop as well, by vectorizing, and make this even more efficient? I tried stuff like:
But that doesn't give a correct result.
To further clarify what I need, I will give an example:
Now I want to count the different values of label_lin in clusters_size (this is what the for loop does), so that:
If I do something like:
I will instead get:
Which is obviously incorrect.


Sign in to comment.




1 Answer

Answer by dpb
on 31 Aug 2019

>> histc(label_lin,unique(label_lin))
ans =


This pretty much seems to be in agreement with what I tested, so I'll probally stay with the loop for now. After testing run times for different parts of my calculation, I'm now trying to make the part of the calculation that takes the most run time (especially for large L) more efficiently - see my previous comment above.
on 1 Sep 2019
Yeah, has often been noted that histc is not as efficient as could be--I just wish TMW had chosen to work on it instead or at least not changed binning behavior in introducing histcounts. This incessant introduction of overlapping functionality and inconsistent syntax and behavior is really a pain and source of both confusion and error.
As far as "large", a double array of 512^3 is 128 MB. Once upon a time, that would have taxed the largest machines, now "entry-level" is 4GB physical memory. The key item is that the operation is inside a looping construct. However, as you note above, that's still not a significant fraction of the time in the loop so even if it were cut to nil it wouldn't make all that much difference. The need to optimize brings along the need to find out what areas offer real gains overall, not just "peephole" optimization.
Looking at CC2periodic you could also profile it...not sure which output form you're using; notice that one branch uses accumarray and sort both...there might be some opportunity there.
Whether it would suit your need or not, noticed a C source on GitHub that might be of some use if wanted to write a mex file.
It's not too hard to write your own version of bwconncomp or bwlabel so you could make it wrap around the edges. Whether or not an m file implementation will gain you anything over what you're using now remains to be seen. Certainly, if it were coded as a mex you could see a gain.

Sign in to comment.