Remove NaNs in matrices within cell array

조회 수: 5 (최근 30일)
Hannu K
Hannu K 2013년 11월 7일
편집: Andrei Bobrov 2013년 11월 7일
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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 7일
편집: Andrei Bobrov 2013년 11월 7일
out = cellfun(@(x)x(~isnan(x)),num2cell(A,1),'un',0);
with for-end loop
n = size(A,2);
out = cell(n,1);
for jj = 1:n
out{jj} = A(~isnan(A(:,jj)),jj);
end
with accumarray
s = size(A);
ii = ones(s(1),1)*(1:s(2));
out = accumarray(ii(:),A(:),[],@(x){x(~isnan(x))});
  댓글 수: 1
Hannu K
Hannu K 2013년 11월 7일
Thank you! As it turns out, the for-loop approach is quicker.

댓글을 달려면 로그인하십시오.

추가 답변 (6개)

Matt J
Matt J 2013년 11월 7일
편집: Matt J 2013년 11월 7일
This does the trick, but I'm worried about the performance as A can be huge in my actual code.
Cellfun is usually less efficient than a for-loop. It basically runs a for-loop internally, but in a slightly less efficient way than a direct for-loop.
Also, even though A is huge, length(ct) will not be, correct? If length(ct) is really huge, that's a bigger problem than the question of whether to use cellfun or for-loops. It means you'll have lots of data scattered discontiguously in RAM and access to the cell contents will be very slow.
  댓글 수: 1
Hannu K
Hannu K 2013년 11월 7일
Thank you for the insight. I always thought that cellfun does some magic that outperforms the for loop.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2013년 11월 7일
If all elements of A are either NaN or non-zero, then it would be better to convert to a sparse matrix, rather than use cell arrays
[i,j,s]=find(~isnan(A));
[m,n]=size(A);
A=sparse(i,j,s,m,n);

Matt J
Matt J 2013년 11월 7일
Yet another alternative,
map=~isnan(A);
[i,j]=find(map);
k=histc(j,1:size(A,2));
C=mat2cell(A(map),k,1).';

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 7일
out1=arrayfun(@(x) A(~isnan(A(:,x)),x),1:size(A,2),'un',0)

David Sanchez
David Sanchez 2013년 11월 7일
for single line solution, A. Abdelmalek's is the solution. You can perform the same by using the following:
A = [3 NaN NaN 2 4; NaN NaN NaN 5 NaN; 6 7 NaN 8 NaN];
[rows cols] = size(A); % number of rows and columns in your matrix
D = mat2cell(A,rows,ones(1,cols)); % cell to hold your data
for k=1:numel(D)
C = ~isnan(D{k});
D{k}(C==0) = [];
end
It works with values different to 1 and the code is quite readable.

Bob Freeman
Bob Freeman 2013년 11월 7일
do find(NaN == 0)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by