Sort cells order according to their values?

조회 수: 2 (최근 30일)
Xiaohan Du
Xiaohan Du 2018년 9월 6일
답변: Stephen 2018년 9월 6일
Hi all,
I have a function produces a cell like this:
K>> otpt{:}
ans =
3 0
2 1
ans =
1 -1
4 -0.5
ans =
4 -0.5
3 0
In each cell, the first column are labels and second column are x-coordinate of nodes. The output is a division of [-1, 1] into a number of blocks. As you can see this example equals to
The labels remain fixed for the nodes, however each time the function gives different order of cells. Now whenever I get the output, I'd like to sort the order of cells from left to right, i.e. I want the output to be
K>> otpt{:}
ans =
1 -1
4 -0.5
ans =
4 -0.5
3 0
ans =
3 0
2 1
How can I do it? Thanks!

채택된 답변

Stephen
Stephen 2018년 9월 6일
I'm not sure why your desired output is being spread over two function outputs, but I believe you should be using the "sort" function with the optional second output in this scenario.
syntax:
[B,IX] = sort(A)
In your scenario, I'll assume you can do the sort inside your function on a common variable. I'll call that variable "list". You want to sort the variables in the second column of the variable.
[B2,IX] = sort(list(:,2));
B1 = list(IX,1);
output = [B1,B2];
After that you can parse the variable "output" as necessary to get your multiple outputs.

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