Sort cells order according to their values?

1 次查看(过去 30 天)
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