I have this following loop for a code (that computes the histograms). I am writing in Matlab. As I am new to Matlab I don’t know of any faster methods to do this. I am currently writing
for i=1:size(b)
a(b(i)) = a(b(i)) + 1;
end
Are there any faster methods to do this, preferably those which do not require the for loop?
You can simply vectorize it by
a(b) = a(b) + 1. Check the following:If you use some indices multiple times, then
accumarraywill help as follows:Alternatively, you can use:
See this nice answer here for further details.