MATLAB: Column operation based on frequency of element in a mtrix

frequency of elements in matrixunique value of a vector in a matrix

Suppose I have a matrix like this:
x=[ 1 2 4 5 6; 2 2 3 3 3]
and based on the frequency of elements in the 2nd column I need to make summation to all perspective elements in the 1st column The desired output is:
y=[3 15; 2 3]
The first column: 3= 2+1 & 15= 4+5+6, The second column: unique values of x
Thank you in advance

Best Answer

>> u=unique(x(2,:)));
>> [~,bin]=histc(x(2,:),u);
>> [accumarray(bin.',x(1,:)).';u]
ans =
3 15
2 3
>>