Hi
I want to sort this structure in descensind order using MATLAB 2018:
ten.cat=[7,2,4,1].
I used the following commands but it is still not sorted:
pp_1=ten;
TT = struct2table(pp_1) % convert to tables
gg=sortrows(TT,'cat') % sort
sortedpp_1 = table2struct(gg) % convert the table back to structure.
please, help

댓글 수: 3

Jan
Jan 2019년 5월 29일
편집: Jan 2019년 5월 29일
What is your question and with which problem can we help you? What exactly is not sorted?
Why not sorting it by:
ten.cat = [7,2,4,1];
ten.cat = sort(ten.cat)
Guillaume
Guillaume 2019년 5월 29일
@Jan, this should go as an answer as I see no way of improving on that with the information given.
@shamsuddeen, there's never any need to convert a structure to anything in order to operate on it. Certainly, converting a structure into a table and back is a complete waste of time for your purpose. In this case, the fact that ten is a structure is completely irrelevant anyway, you're not sorting a structure, but just one field of a scalar structure which is the same as any non-structure variable.
Thank u. it works

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

 채택된 답변

Jan
Jan 2019년 5월 29일

0 개 추천

ten.cat = [7,2,4,1];
ten.cat = sort(ten.cat)

추가 답변 (1개)

Simon Mählkvist
Simon Mählkvist 2019년 5월 29일
편집: Simon Mählkvist 2019년 5월 29일

0 개 추천

Hi,
Adjusting:
ten.cat=[7,2,4,1]
to:
ten.cat=[7;2;4;1]
gives what I think is the sought after results.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2019년 5월 29일

답변:

Jan
2019년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by