replace specific empty columns by 0 in cell MATLAB?

I have a 54x 15 dimesion cell arry. I want to replace NaNs in some columns (Columns 5-15) as 0.
my data looks like this.

 採用された回答

Voss
Voss 2022 年 5 月 13 日

0 投票

% reproduction of your cell array:
C = repmat({NaN},7,15);
C(1:4,11:15) = {0};
C(5:7,5:15) = {0};
C{1,1} = '0.x';
C{2,2} = 29024315;
C{5,2} = 1008158;
C{6,3} = 24726.21;
C{6,14} = 2;
disp(C);
{'0.x'} {[ NaN]} {[ NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[29024315]} {[ NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ 1008158]} {[ NaN]} {[NaN]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[2.4726e+04]} {[NaN]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[0]} {[0]} {[0]} {[2]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[0]} {[0]} {[0]} {[0]} {[0]}
% replace (scalar) NaNs in columns 5 through 15 of C with zeros:
temp = C(:,5:15);
temp(cellfun(@(x)isscalar(x) && isnan(x),temp)) = {0};
C(:,5:15) = temp;
disp(C);
{'0.x'} {[ NaN]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[29024315]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ 1008158]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[NaN]} {[ NaN]} {[2.4726e+04]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[2]} {[0]} {[NaN]} {[ NaN]} {[ NaN]} {[NaN]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}

2 件のコメント

Maham Hamid
Maham Hamid 2022 年 5 月 13 日
Thank you so much
Voss
Voss 2022 年 5 月 13 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDebugging and Analysis についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 5 月 13 日

コメント済み:

2022 年 5 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by