how to sort the rows in an cell array.

조회 수: 2 (최근 30일)
jaah navi
jaah navi 2021년 6월 24일
댓글: jaah navi 2021년 6월 25일
I am having cell array of size 3x1
[5,6]
[1,2]
[5,8]
could anyone help me how to sort the rows to get the result in the following manner
[1,2]
[5,6]
[5,8]
  댓글 수: 1
Scott MacKenzie
Scott MacKenzie 2021년 6월 24일
It would help if you posted the code that generates the cell array you want to sort.

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

채택된 답변

Mohammad Sami
Mohammad Sami 2021년 6월 25일
You can try this out.
B = {[5,6] [1,2] [5,8]};
[~,i] = sort(cellfun(@(x)sum(x.*power(10,[length(x):-1:1]-1)),B));
B = B(i)
B = 1×3 cell array
{[1 2]} {[5 6]} {[5 8]}
  댓글 수: 3
Mohammad Sami
Mohammad Sami 2021년 6월 25일
This is just converting the array into a decimal value so [5,6] to 56. So makes it easier to sort the numbers. Though you can replace it with the maximum value you expect to be present in your array and it should still work well enough.
B = {[5,6] [1,2] [5,8]};
cellfun(@(x)sum(x.*power(10,[length(x):-1:1]-1)),B)
ans = 1×3
56 12 58
jaah navi
jaah navi 2021년 6월 25일
Thanks for your explanation.

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

추가 답변 (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