필터 지우기
필터 지우기

How to sort a cell array based on the first row?

조회 수: 5 (최근 30일)
Dongyan Zhu
Dongyan Zhu 2021년 6월 18일
댓글: Dongyan Zhu 2021년 6월 18일
I have two rows of cell array:
Combined =
2×3 cell array
{'4'} {'1'} {'6'}
{'1'} {'3'} {'4'}
Now I want to sort the first row of this 2*3 cell array from small number to large number and the second row will be sorted according to the first row, which means the second row should look like this:
{'1'} {'4'} {'6'}
{'3'} {'1'} {'4'}
Then I want to extract the second row as an 1*3 cell array.
Can someone help me? Many thanks.

채택된 답변

the cyclist
the cyclist 2021년 6월 18일
Out of curiosity, is there some reason you are using a cell array rather than a numeric array to store these numeric data?
% Original data
in = {'4','1','6';'1','3','4'}
in = 2×3 cell array
{'4'} {'1'} {'6'} {'1'} {'3'} {'4'}
% Sort first row, and get sorting index
[in_sorted,sorting_idx] = sort(in(1,:))
in_sorted = 1×3 cell array
{'1'} {'4'} {'6'}
sorting_idx = 1×3
2 1 3
% Extract last row, sorted according to first
out = in(2,sorting_idx)
out = 1×3 cell array
{'3'} {'1'} {'4'}
  댓글 수: 1
Dongyan Zhu
Dongyan Zhu 2021년 6월 18일
Thank you! I just want to directly use the cell array for the data transfer later in the task.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by