How to rearrange a matrix according to the repitition of the elements/arrays in the matrix?
3 views (last 30 days)
Show older comments
KARTHIK AMBIKAKUMARI SANALKUMAR
on 4 Nov 2019
Commented: KARTHIK AMBIKAKUMARI SANALKUMAR
on 5 Nov 2019
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
0 Comments
Accepted Answer
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)
See Also
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!