Sorting a cell array?
I have a cell array of numbers but the majority of the cell array is empty for example:
x =
[] [6] [] [4] [] [] [] [1]
I have a matching array y
y = [1, 3,1,5,7,3,1,5]
I want to get the index of the numbers from the cell array x and use them to get the corresponding values from y. So x(2) matches with y(2). I convert x to a array using
x = cell2mat(x);
But the problem is that it returns
x = [6,4,1]
开发者_Go百科
This does not allow me to get the correct index so I can then sort X and then sort Y accordingly so the same indices match up. I tried to use sort that does not work for cell arrays.
Just use y(x)
; that will return indices 6, 4, and 1 from the y
vector.
Note that the order of the returned matrix will depend on the order of the indices in x
; if you want to sort x
, do it before running y(x)
.
精彩评论