如何按浮点列对 Octave 中的元胞数组进行排序?

发布于 2024-12-28 13:39:20 字数 310 浏览 1 评论 0原文

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

sortrows (mycellarray, 4);

但是如果我要排序的列是浮点数列em>,然后我收到此错误消息:

error: sort: only cell arrays of character strings may be sorted

有谁知道如何按浮点列对元胞数组进行排序?

I created a cell array in Octave. Some columns contain floats, and some columns contain strings. I am able to sort the cell array by a column of strings (say col #4), using this command:

sortrows (mycellarray, 4);

But if the column I want to sort by is a column of floats, then I get this error message:

error: sort: only cell arrays of character strings may be sorted

Does anyone know how to sort a cell array, by a column of floats?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-01-04 13:39:20

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

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,:);

然而,在 Octave 的某些版本中,未定义波形符 ~ 运算符。在这种情况下:

vector2sort = mycellarray(:,3); 
[dummy,idx] = sort(vector2sort); 
mycellarraysorted = mycellarray(idx,:);

Convert the column with float values to a vector, sort it and get the sorting index. Then you can apply this index to your cell array.

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,:);

In some versions of Octave, however, the tilde ~ operator is not defined. In that case:

vector2sort = mycellarray(:,3); 
[dummy,idx] = sort(vector2sort); 
mycellarraysorted = mycellarray(idx,:);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文