Andi
Andi

Reputation: 4899

MATLAB: Replace column in cell array with NaN

I have a 3x2 cell array data where each cell contains a double matrix. It looks something like this:

{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}
{600x4 double} {600x4x3 double}

Now, I would like to replace the second column of the cell array with NaNs. The result should therefore look like this:

{600x4 double} {[NaN]}
{600x4 double} {[NaN]}
{600x4 double} {[NaN]}

Using curly braces is leading nowhere.

data{:,2} = nan
Expected one output from a curly brace or dot indexing expression, but there were 3 results.

I think I could use cellfun or a simple for-loop to change the values to NaN. However, I would like to know if there's a more simple solution?

Upvotes: 1

Views: 272

Answers (1)

Wolfie
Wolfie

Reputation: 30175

You can use this

data(:,2) = {NaN};

Logic:

% Assign all values in the 2nd column of the cell ...
% All elements should be the single cell {NaN};

You can alternatively do this (slightly clearer logic)

data(:,2) = repmat( {NaN}, size(data,1), 1 ); % Right hand side is same height as data

Or even this!

data(:,2) = num2cell( NaN(size(data,1), 1 ) );

Upvotes: 3

Related Questions