I have a for loop performing the current operation:
T = [1,1,1,4,5,6,3];
A = [20,15,4,21,14,3];
l = length(T);
% how can we vectorize this?
for(i = 1:l)
A(T(i)) = A(T(i)) + 1;
end
Simply put, it uses the T vector as a list of indices to increment on the A array in a particular order. For instance, the first element in array A will be incremented 3 times (corresponding to the 3 ones in T), while the rest get incremented once, 2 doesn't get incremented. The resulting changes to A are thus:
A = [23,15,5,22,15,4];
However, ideally I would like to avoid a for loop here. Before I tried:
A(T) = A(T) + 1;
This didn't work; MATLAB just ignores the repeated indices. Is there someway I can perform the operation in the for loop by vectorisation or some other means?