MATLAB: How to rearrange a matrix according to the repitition of the elements/arrays in the matrix

matrixmatrix arraysrearranging matrix elements

Hello
I have a matrix of size (500×6).
First 3 coloumns are the 3 co-ordinates and the last 3 are the corresponding vectors.
But the matrix has repeating co-ordinates (first 3 coloumns) which has either x-vector value (4th colomn and other vectors zeros) or y-vector value (5th colomn and other vectors zeros).
So how can I combine these repeating co-ordinates into one array so that it has corresponding x-vector, y-vector values.
(For ex. A = (1 2 3 574 0 0; 1 2 3 0 -95 0) to A = (1 2 3 574 -95 0 ) )
Thanks in advance

Best Answer

example solution:
A=[1 2 3 574 0 0; 1 2 3 0 -95 0; 4 5 6 580 0 0; 4 5 6 0 200 0];
[z, ~, di]=unique(A(:,1:3),'rows');
newa=[];
for k=1:max(di)
newa=[newa; z(k,:) sum(A(di==k,4:6))];
end
disp(newa)