sort a structure

조회 수: 3 (최근 30일)
Philip
Philip 2011년 9월 28일
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.

채택된 답변

Daniel Shub
Daniel Shub 2011년 9월 28일
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
Philip
Philip 2011년 9월 28일
Thanks! That worked perfectly!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by