Sort matrix based on unique values in one column
Show older comments
Hi guys. I can't seem to overcome my problem. I have a matrix with 5 columns and a lot of rows. I need to sort the whole matrix based on unique values in column 1. How do I do that? So I need to find the unique values (its a timestamp from my experiment) in column one and also the corresponding values in the rest of the columns and everything else needs to be deleted. Thanks!
Accepted Answer
More Answers (2)
Guillaume
on 14 Apr 2016
I'm not sure what you mean by "and everything else needs to be deleted". Does that means that rows with identical first column also have the exact same values for all the other columns, or that you want to somehow merge these identical rows into just one?
Assuming all duplicate rows are identical (but then why would you say unique values in one column?):
b = unique(a, 'rows');
Assuming you want to just keep one arbitrary row out of the ones that have identical first column:
[~, rows] = unique(a(:, 1));
b = a(rows, :);
If you want to somehow merge the duplicate rows, it probably can be done but you need to give more details.
1 Comment
STAVRINI TSANGARI
on 23 Oct 2020
how can I get the average of the two duplicate columns?
Mohammad Bhat
on 15 Feb 2018
1 vote
sir, If we want to keep only maximum value besides second unique column value...
Categories
Find more on Shifting and Sorting 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!