Sort cells order according to their values?

1 Ansicht (letzte 30 Tage)
Xiaohan Du
Xiaohan Du am 6 Sep. 2018
Beantwortet: Stephen am 6 Sep. 2018
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!

Akzeptierte Antwort

Stephen
Stephen am 6 Sep. 2018
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.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by