I have a matrix that has some data in its columns like this:
A = [1 NaN NaN 1 1; NaN NaN NaN 1 NaN; 1 1 NaN 1 NaN]
I want to extract the non NaN elements so that they are returned in a cell array where a cell holds the non NaN elements of the columns like this:
C = {[1;1],[1],[],[1;1;1],[1]}
This far I have tried to use the cellfun:
C = num2cell(A,1); C(cellfun(@isnan,C, 'UniformOutput', false)) = [];
But i receive an error: Function 'subsindex' is not defined for values of class 'cell'.
I Managed to use for loop:
C = num2cell(A,1);for i = 1:length(ct) inds = isnan(ct{i}); ct{i}(inds) = [];end
This does the trick, but I'm worried about the performance as A can be huge in my actual code.
Any help is more than welcome!
Edit: typo
Best Answer