Borrar filtros
Borrar filtros

Table back to its original form?

22 visualizaciones (últimos 30 días)
wesleynotwise
wesleynotwise el 8 de Jun. de 2017
Comentada: breathi el 15 de Nov. de 2021
So one can sort the data in any column of a table in an ascending / descending order from the drop-down list on the heading. However, is there a way to turn the sorted table back to its original form (unsorted)?

Respuestas (2)

Star Strider
Star Strider el 8 de Jun. de 2017
If you kept the index vector from the sorted data, you can ‘unsort’ them:
x = randi(99, 10, 1); % Original Data
[xs,idx] = sort(x); % Sorted Data & Index
x_unsort(idx) = xs; % ‘Unsorted’ Data
If you didn’t keep it, all hope is lost.
  1 comentario
Guillaume
Guillaume el 8 de Jun. de 2017
If you didn’t keep it, all hope is lost
Which is exactly the case (didn't keep it) if you use the UI to do the sorting. You'll notice that matlab outputs the command it uses to do the sorting to the command window, and it is of the form:
yourtable = sortrows(yourtable, columnname, direction);
whereas you'd need
[yourtable, order] = sortrows(...)
to be able to restore the order.
There's no workaround other than doing the sorting through the command window rather than the UI, unfortunately.

Iniciar sesión para comentar.


KSSV
KSSV el 8 de Jun. de 2017
Editada: KSSV el 8 de Jun. de 2017
Read about Sort. You have to do that using a command/ function sort.
k = [3 4 5 1 2 3 6] ;
[val,idx] = sort(k) ;
Your sorted k is val and your original data remains same as k.
  3 comentarios
Peter Perkins
Peter Perkins el 9 de Jun. de 2017
If by "in MATLAB", you mean "at the MATLAB command line", others have provided the answer: use the second output of sort. If you mean "in the Variable Editor", can you not use the Undo button or ctrl-z?
breathi
breathi el 15 de Nov. de 2021
@Peter Perkins: You can use Ctrl+Z or the Undo button, but only once. Once you make 2 sort commands in the Variable Editor, it's over.

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by