How to sort an array of cells in Octave with a float column?

I created an array of cells in Octave. Some columns contain floats, and some columns contain rows. I can sort the array of cells with a column of rows (e.g. col # 4) using the following command:

sortrows (mycellarray, 4); 

But if the column I want to sort is a floating point column, then I get this error message:

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

Does anyone know how to sort an array of cells using a floats column?

+6
source share
1 answer

Convert a column with float values ​​to a vector, sort it and get the sort index. You can then apply this index to an array of cells.

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

However, in some versions of Octave, the tilde ~ operator is not defined. In this case:

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

Source: https://habr.com/ru/post/906703/


All Articles