1

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?

1 Answer 1

3

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 ) );
Sign up to request clarification or add additional context in comments.

3 Comments

Also [data{:,2}] = deal(NaN)
@Wolfie Your first suggestion is straight forward. Actually very easy ;-) Thanks.
@Luis damn I couldn't get that one to click, I was missing the deal!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.