首页 > 解决方案 > MATLAB:用 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. 但是,我想知道是否有更简单的解决方案?

标签: matlabcell-array

解决方案


你可以用这个

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 ) );

推荐阅读