How to remove duplicate x-values and rearrange the y-values?
Show older comments
Dear Matlab users,
I would like to resolve with your help the following problem. I have multiple columns filled with x and y values in repeating order such as:
Col(1): x1 x1 x1 x2 x2 x3 x4 x4
Col(2): y1 y2 y3 y4 y5 y6 y7 y8
Col(n-1): x1 x1 x2 x3 x3
Col(n): y1 y2 y3 y4 y5
How would it be possible to rearrange the data into a form where the Col(1) would contain x-values without duplicates with corresponding y-values in the remaining columns? Something like:
Col(1): x1 x2 x3 x4
Col(2): y1 y4 y6 y7
Col(3): y2 y5 y4 y8
Col(4): y3 y3 y5
Col(5): y1
Col(6): y2
Your help is greatly appreciated. Thank you.
Marian
Accepted Answer
More Answers (2)
Walter Roberson
on 9 May 2022
g = findgroups(x)
Col = splitapply(@(Y) {Y}, y, g) ;
use
yourMat=[ [4; 1; 1; 2; 3; 3; 5; 10 ], rand(8,4)];
[~,index]=unique(yourMat(:,1),'stable'); % get unique values from the first column of your matrix/cell without sorting the values
diminishedMat=yourMat(index,:)
1 Comment
Marián Matejdes
on 9 May 2022
Edited: Marián Matejdes
on 9 May 2022
Categories
Find more on Resizing and Reshaping Matrices 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!