Sort matrix based on unique values in one column

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!

 Respuesta aceptada

Stephen23
Stephen23 el 14 de Abr. de 2016
Editada: Stephen23 el 14 de Abr. de 2016
[~,idx] = sort(mat(:,1)); % sort just the first column
sortedmat = mat(idx,:); % sort the whole matrix using the sort indices
You can even do the same thing with unique to get the rows corresponding to unique values in the first column:
[~,idu] = unique(mat(:,1))
uniquerows = mat(idu,:);
Note that unique has options for controlling the order of its outputs, and whether it takes the first or last matching element. You should check its documentation and pick the best options for you requirements.

11 comentarios

That's a bit overkill. The same can be achieved simply with:
sortedmat = sortrows(mat, 1); %you don't even need the ,1 for 1st column
However, I don't think that's what the OP is asking (since he mentions using unique values).
Ida Rosendahl
Ida Rosendahl el 14 de Abr. de 2016
Ah, thanks a lot. Using 'unique' instead of 'sort' in your answer totally solved my problem.
Awesome..it worked for me too! Thanks a lot Guillaume
Thank you, it was lifesaver
Thanks. It sorts correctly. I am happy.
Mike King
Mike King el 16 de Abr. de 2020
Thank You
Adam Ward
Adam Ward el 24 de Jun. de 2020
Thank you so much!
Emma Warner
Emma Warner el 1 de Jul. de 2020
Just what I was looking for. Thank you!
you saved my life, thnks
thank you so much
Ranjith K
Ranjith K el 15 de Sept. de 2022
[~,idu] = unique(mat(:,1))
how to mention the name inside of 1 2 ....10
For example B1 B2 B3.............B10

Iniciar sesión para comentar.

Más respuestas (2)

Guillaume
Guillaume el 14 de Abr. de 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.
Mohammad Bhat
Mohammad Bhat el 15 de Feb. de 2018
sir, If we want to keep only maximum value besides second unique column value...

Categorías

Etiquetas

Preguntada:

el 14 de Abr. de 2016

Comentada:

el 15 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by