replacing NaN with zeros in a cell column of strings
이전 댓글 표시
Dear all,
I have this column
A={[NaN]
[NaN]
[NaN]
[NaN]
[NaN]
'ka'
[NaN]
[NaN]
'jdfksjkg'
'ldsiu'
[NaN]
[NaN]
'jdfkkg'
'jdfkkg'
};
and I want to replace the NaN with zeros that is ,
A={[0]
[0]
[0]
[0]
[0]
'ka'
[0]
[0]
'jdfksjkg'
'ldsiu'
[0]
[0]
'jdfkkg'
'jdfkkg'
};
Is there a way of doing that in matlab?
Thanks in advance
채택된 답변
추가 답변 (2개)
Wayne King
2013년 4월 24일
One way is just with a for loop:
for ii = 1:length(A)
if isnan(A{ii})
A{ii} = 0;
end
end
댓글 수: 4
antonet
2013년 4월 25일
Image Analyst
2013년 4월 25일
편집: Image Analyst
2013년 4월 25일
Wow, 5 lines is too complicated?!? Any shorter way is likely to be more obtuse, not as straightforward and easy to follow, and harder to maintain because it'd be more cryptic. What's wrong with a loop, especially for only 14 variables? I mean, it's not like you have tens of millions or anything, so speed is definitely NOT going to be an issue.
Jan
2013년 4월 25일
This is the most simplest method. +1
Shane Hagen
2015년 3월 22일
편집: Shane Hagen
2015년 3월 22일
isnan doesnt work for cells...can anyone help?
If i separate out the individual cell with NaN it works but not in the loop
ix=cellfun(@isempty,spiketimes);
spiketimes(ix)={0} ; %which I wish would just put 0's
for ii = 1:length(spiketimes)
if isnan(spiketimes{ii})
spiketimes{ii} = 0;
end end
results = spiketimes{j};
%n = length(results(j,:));
plot([results{:} ; results{:}], [ones(1,n)*j-1;ones(1,n)*j],'k-'); end; end;
Undefined function 'isnan' for input arguments of type 'cell'.
this is at the end of my code..would appreciate any help
You can go for something like:
A(cellfun(@(x)all(x~=x), A)) = {0} ;
but it is just "cosmetic" (in some sense) and the loop proposed by Wayne (with a little update so it manages well non-scalar content) is likely to be more efficient.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!