如何通过一列浮点数对Octave中的单元格数组进行排序?

moo*_*dog 6 sorting octave

我在Octave中创建了一个单元阵列.有些列包含浮点数,有些列包含字符串.我可以使用以下命令按字符串列(例如col#4)对单元格数组进行排序:

sortrows (mycellarray, 4);
Run Code Online (Sandbox Code Playgroud)

但是,如果我要排序的列是一列浮点数,那么我收到此错误消息:

error: sort: only cell arrays of character strings may be sorted
Run Code Online (Sandbox Code Playgroud)

有谁知道如何通过浮动列对单元格数组进行排序?

yuk*_*yuk 6

将具有浮点值的列转换为向量,对其进行排序并获取排序索引.然后,您可以将此索引应用于您的单元格数组.

mycellarray = {'a',1,0.5; 'b',2,0.1; 'c',3,4.5; 'd',4,-3.2};
vector2sort=cell2mat(mycellarray(:,3));
[~,idx] = sort(vector2sort)
mycellarraysorted = mycellarray(idx,:);
Run Code Online (Sandbox Code Playgroud)

但是,在某些版本的Octave中,~未定义波浪号运算符.在这种情况下:

vector2sort = mycellarray(:,3); 
[dummy,idx] = sort(vector2sort); 
mycellarraysorted = mycellarray(idx,:);
Run Code Online (Sandbox Code Playgroud)