sort a structure

4 Ansichten (letzte 30 Tage)
Philip
Philip am 28 Sep. 2011
Is it possible to create a structure that holds the following information:
  • id_number
  • length
such that I can then sort the structure by length from largest to smallest. I then want to retrieve the id_number for the top 5 elements.

Akzeptierte Antwort

Daniel Shub
Daniel Shub am 28 Sep. 2011
something like:
s = struct('id_number', mat2cell(1:100, 1, ones(100, 1)), 'length', mat2cell(rand(1, 100), 1, ones(100, 1)));
[a, b] = sort([s.length]);
[s(b(1:5)).id_number]
  1 Kommentar
Philip
Philip am 28 Sep. 2011
Thanks! That worked perfectly!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by