replacing NaN with zeros in a cell column of strings

1 回表示 (過去 30 日間)
antonet
antonet 2013 年 4 月 24 日
編集済み: Shane Hagen 2015 年 3 月 22 日
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

採用された回答

Jan
Jan 2013 年 4 月 25 日
Are the NaNs the only non-CHAR elements? Then:
A(~cellfun('isclass', A, 'char')) = {0};
  1 件のコメント
antonet
antonet 2013 年 4 月 25 日
thanks Jan. I have columns of dimension 200000 by 1. so I want to avoid using loops

サインインしてコメントする。

その他の回答 (2 件)

Wayne King
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 件のコメント
Jan
Jan 2013 年 4 月 25 日
This is the most simplest method. +1
Shane Hagen
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

サインインしてコメントする。


Cedric
Cedric 2013 年 4 月 25 日
編集済み: Cedric 2013 年 4 月 25 日
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.
  2 件のコメント
antonet
antonet 2013 年 4 月 25 日
Any other sugestion?
thanks
Jan
Jan 2013 年 4 月 25 日
@antonet: You got two working solutions. Asking for more solutions without explaining, why you are not satisfied already, is not efficient. Please do not let us guess, what you are exactly looking for, because this wastes your and our time.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by