Sorting an array of cells with empty elements in Matlab

I have an array of cells in Matlab DataVal that I would like to sort. Some of the elements in the array are empty. Any idea how to sort the elements of this array of cells. When sorting, I would prefer empty elements at the beginning or end of the sorted array.

 DataVal ans = [] [ 82.1000] [ 16.1500] [ 56.0200] [] [ 74.9600] [ 35.1400] 

I used sort and sortrows . Both of these commands seem to ignore empty elements.

thanks

+4
source share
1 answer

How about this:

 x = { [] [ 82.1000] [ 16.1500] [ 56.0200] [] [ 74.9600] [ 35.1400] }; x_sorted = [cell(sum(cellfun(@isempty,x)),1) ; num2cell(sort(cell2mat(x)))] 

Result:

 x_sorted = [] [] [16.15] [35.14] [56.02] [74.96] [ 82.1] 

First, we convert the array of cells into a vector of values, sort them, and then return them back as an array of cells. Finally, we add the original number of empty cells at the beginning, as cell2mat ignores them in this case.

+6
source

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


All Articles