I'm new to MATLAB and learning to use vector expressions instead of verbose for loops. I have a snippet and I was wondering whether it could even be written in a concise vector and if so how would I modify it.
for v = I
X(i, v) = X(i, v) + length(I(I == v));
end
X
is 1500x200
I
is 3763x1
i
can be thought of as a constant
What I'm doing here is this. I
contains column indexes of X
and I want to increment those locations by the number of times that particular index appeared in I
. So after this for loop is done the i
th row of X
will contain a histogram.
Any other ideas or suggestions to improve my MATLAB coding would also be appreciated.
Here's a couple of ways: