3

I have a 50x25 cell array in the variable raw_data. Each cell contains a 200x150 matrix. I have a few NaN values scattered between all those values and I want to set them to zeros to make sure they do not interfere at later stages.

I have tried the following:

raw_data(cellfun(@(x) any(isnan(x), raw_data, 'UniformOutput', false)) = 0

When running the script, I get "Function 'subindex' is not defined for values of class 'cell'". Can anyone help me, please?

Thanks in advance!

1
  • Probably the best way is to use a for loop Commented Jan 15, 2015 at 10:06

1 Answer 1

4

How about this:

cellfun(@(x) nansum(x,ndims(x)+1), raw_data, 'UniformOutput', false)

Note if you're certain you'll only have 2D matrices in raw_data you can replace the ndims(x)+1 with 3.

The idea is to use nansum to sum along the 3rd dimension as this will preserve the shape of the first 2 dimensions and luckily nansum seems to convert NaN to 0 when all the elements being summed are NaN

Sign up to request clarification or add additional context in comments.

1 Comment

Also you can shorten 'UniformOutput', false to 'uni',0 if you like. In fact I think you can even do 'u',0

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.