简体   繁体   English

在多列中对MATLAB单元阵列进行排序

[英]Sort MATLAB Cell Array Across multiple columns

I have data that is of the following form: 我有以下形式的数据:

'of' 45001 23366 21859591 52876216 0 45001
'on' 40649 23226 17940625 44201973 0 40649
'my' 37976 18338 11277975 47042676 0 37976
'me' 28707 18134 6546887 36222235 0 28707

I am wondering how in MATLAB I go about sorting say column 2 from largest to smallest but keep all the other rows associated with the one that's being sorted - this is stored in a cell array also. 我想知道在MATLAB中我如何排序第2列从最大到最小,但保持所有其他行与正在排序的行相关联 - 这也存储在单元格数组中。

Any help would be appreciated. 任何帮助,将不胜感激。

Although you're dealing with a cell array , the answer actually ends up being the same as that listed for numeric arrays in the closely-related questions I linked to in my above comment: simply use the function SORTROWS . 虽然你正在处理一个单元格数组 ,但实际上答案实际上与我在上面评论中链接的密切相关问题中列出的数字数组相同:只需使用函数SORTROWS即可 Here's how you can sort the rows of your cell array according to the values in the second column (in descending order): 以下是如何根据第二列中的值(按降序排序)对单元格数组的行进行排序:

sortedCellArray = sortrows(cellArray,-2);


NOTE: It should be noted that the documentation for SORTROWS doesn't appear to explicitly say that the function will work with cell array inputs, but it does have an example showing that it works for them just like it does for any other array. 注意:应该注意的是, SORTROWS的文档似乎没有明确说明该函数将与单元格数组输入一起使用,但它确实有一个示例显示它适用于它们,就像它对任何其他数组一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM