MATLAB: How to replace same values in an array

histogramMATLABsame values

If I have this couples of values: 1—5; 2—7; 3—4; 2—3; 6—5 and I want that the first column is the x column and the second one is the y column of an xy graph (especially an histogram), how can I make that for the same value of x, y is the sum of y values for the same x as follow? 1—5; 2—10; 3—4; 6—5.
Thank you
Fabio

Best Answer

If
xy=your array;
y=accumarray(xy(:,1),xy(:,2));
See
doc unique
doc accumarray
for how the above works...
>> xy=[1 5;2 4;3 8; 2 6];
>> [unique(xy(:,1)) accumarray(xy(:,1),xy(:,2))]
ans =
1 5
2 10
3 8