I have a cell array in Matlab DataVal
which I would like to sort. Some of the elements in the array are empty. Any idea how to sort the elements of this cell array. When sorted, I would prefer, the empty elements at the beginning or the end of the sorted array.
DataVal
ans =
[]
[ 82.1000]
[ 16.1500]
[ 56.0200]
[]
[ 74.9600]
[ 35.1400]
I used sort
and sortrows
. Both these commands seem to ignore the empty elements.
Thanks
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)))]
The result:
x_sorted =
[]
[]
[16.15]
[35.14]
[56.02]
[74.96]
[ 82.1]
We first convert the cell array into a vector of values, sort them, then put them back as a cell array. Finally we add the original number of empty cells back at the beginning, since cell2mat
ignores them in this case.