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

3 views (last 30 days)
Hello
I have a matrix of size (500x6).
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

Accepted Answer

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 4 Nov 2019
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)

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!