简体   繁体   中英

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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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