I have a vector J which contains the row index of a vector.
I would like to do the following code in a vectorized way : I want to sum the duplicate values to the variable 'Mode' from the variable 'M'. I have size(M)=size(J). This is my attempted code:
Mode=zeros(n,1);
for i=1:length(J)
Mode(J(i))=Mode(J(i))+M(i);
end
I have tested with
Mode(J)=M
but the problem is that there are some duplicate index value in J. How can I implement it correctly ?
accumarray- angainorMode(J(i))=Mode(J(i))+M(i);supposed to beMode(J(i))=Mode(J(i))+M(J(i));? - Matthew Gunn