MATLAB:用 NaN 替换元胞数组中的列
MATLAB: Replace column in cell array with NaN
我有一个 3x2 元胞数组 data
,其中每个元胞包含一个双精度矩阵。它看起来像这样:
{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}
现在,我想用 NaN 替换元胞数组的第二列。因此,结果应如下所示:
{600x4 double} {[NaN]}
{600x4 double} {[NaN]}
{600x4 double} {[NaN]}
使用大括号是行不通的。
data{:,2} = nan
Expected one output from a curly brace or dot indexing expression, but there were 3 results.
我想我可以使用 cellfun
或一个简单的 for 循环将值更改为 NaN
。但是,我想知道是否有更简单的解决方案?
你可以使用这个
data(:,2) = {NaN};
逻辑:
% Assign all values in the 2nd column of the cell ...
% All elements should be the single cell {NaN};
你也可以这样做(逻辑更清晰)
data(:,2) = repmat( {NaN}, size(data,1), 1 ); % Right hand side is same height as data
甚至这个!
data(:,2) = num2cell( NaN(size(data,1), 1 ) );
我有一个 3x2 元胞数组 data
,其中每个元胞包含一个双精度矩阵。它看起来像这样:
{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}
现在,我想用 NaN 替换元胞数组的第二列。因此,结果应如下所示:
{600x4 double} {[NaN]}
{600x4 double} {[NaN]}
{600x4 double} {[NaN]}
使用大括号是行不通的。
data{:,2} = nan
Expected one output from a curly brace or dot indexing expression, but there were 3 results.
我想我可以使用 cellfun
或一个简单的 for 循环将值更改为 NaN
。但是,我想知道是否有更简单的解决方案?
你可以使用这个
data(:,2) = {NaN};
逻辑:
% Assign all values in the 2nd column of the cell ...
% All elements should be the single cell {NaN};
你也可以这样做(逻辑更清晰)
data(:,2) = repmat( {NaN}, size(data,1), 1 ); % Right hand side is same height as data
甚至这个!
data(:,2) = num2cell( NaN(size(data,1), 1 ) );