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!
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!