如何使用 for 循环覆盖 table 中的 'NaN' 字符串
How to overwrite 'NaN' strings in a table using for-loop
我想用 table 中的字符串 'NaN' 使用 循环 或任何其他 returns NaN 的策略替换为 NaN , 其他一切不变。这是我的代码:
for k = 1:height(Z_24TimeSteps)
if isnan(Z_24TimeSteps{k})
Z_24TimeSteps{k} = nan;
end
end
Table:
我尝试的另一种策略:
Z_24TimeSteps(cellfun(@isnan,Z_24TimeSteps))=nan;
如何将此原理用于 "table" 格式?
一种可能的方法是在 table 列上使用逻辑索引,如下所示。
Z_24TimeSteps.x1COVGY(strcmp(Z_24TimeSteps.x1COVGY,'NaN')) = {nan}
对于多列,您可以按如下方式循环列。
colNames = Z_24TimeSteps.Properties.VariableNames;
for k = 1:numel(colNames)
if iscell(Z_24TimeSteps.(colNames{k}))
Z_24TimeSteps.(colNames{k})(strcmp(Z_24TimeSteps.(colNames{k}),'NaN')) = {nan};
else
Z_24TimeSteps.(colNames{k})(strcmp(Z_24TimeSteps.(colNames{k}),'NaN')) = nan;
end
end
我想用 table 中的字符串 'NaN' 使用 循环 或任何其他 returns NaN 的策略替换为 NaN , 其他一切不变。这是我的代码:
for k = 1:height(Z_24TimeSteps)
if isnan(Z_24TimeSteps{k})
Z_24TimeSteps{k} = nan;
end
end
Table:
我尝试的另一种策略:
Z_24TimeSteps(cellfun(@isnan,Z_24TimeSteps))=nan;
如何将此原理用于 "table" 格式?
一种可能的方法是在 table 列上使用逻辑索引,如下所示。
Z_24TimeSteps.x1COVGY(strcmp(Z_24TimeSteps.x1COVGY,'NaN')) = {nan}
对于多列,您可以按如下方式循环列。
colNames = Z_24TimeSteps.Properties.VariableNames;
for k = 1:numel(colNames)
if iscell(Z_24TimeSteps.(colNames{k}))
Z_24TimeSteps.(colNames{k})(strcmp(Z_24TimeSteps.(colNames{k}),'NaN')) = {nan};
else
Z_24TimeSteps.(colNames{k})(strcmp(Z_24TimeSteps.(colNames{k}),'NaN')) = nan;
end
end