首页 > 解决方案 > 对两个向量数组进行排序以获得循环

问题描述

我正在尝试获取一个排序函数,该函数采用如下数组:

0 2
1 5
2 3
3 0
4 1
5 4

并按周期排序,即它应该输出

0 2
2 3
3 0
1 5
5 4
4 1

是否有内置的东西或者如何以一种精益的方式完成这项工作?

标签: sortingoctave

解决方案


这有效

y=[]; %% results go here
k=1;
while length(x)>0 %% items left? loop 
    _x=x(k,:); %% local copy
    y=[y ; _x]; %% append
    x(k,:)=[]; %% remove from array
    try
        k=find(x(:,1)==_x(2)); %% find next
    catch
        k=1; %% no next? complete cycle, start over
    end     
end

推荐阅读