MATLAB: Vector confusion

vectors

I have a vector A whose values are indices for vector P.
Say A = 2 occurs 10 times.
If I do:
P(A) = P(A) + 1
P(2) = 1 % not 10
I believe I understand why this is happening. Is there a solution without going into FOR loops? A could be quite large.

Best Answer

Especially if you are starting with an all-zero array, use
P = accumarray(A(:),1);