in MATLAB I have a cell array like this
a = { 1 2 2 3 4 5 [] []
2 4 5 4 3 2 4 5
4 5 4 3 4 [] [] []}
I want to remove empty elements in a way that I get this :
a = { 1 2 2 3 4 5 2 4 5 4 3 2 4 5 4 5 4 3 4}
but when I use this : a(cellfun(@isempty,a)) = [];
what I get is this :
a = {1 2 4 2 4 5 2 5 4 3 4 3 4 3 4 5 2 4 5}
which is not what I want
The problem is that the linear index runs in the direction of rows, i.e. it runs through the first conlumn, then through the second column etc.
You can see this when you call
reshape
on a vector:To achieve the result you want, you need to transpose
a
before indexing into it.You can try this : A(~cellfun('isempty',A))