Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Replace NaN in cell array with zero

Subject: Replace NaN in cell array with zero

From: Cat

Date: 10 Sep, 2010 13:40:10

Message: 1 of 6

Hello everybody,

I'm having a wee problem trying to figure out how to replace all NaN's in my cell array with a value of zero.

A simplified version of my array looks something like this:
x=
[1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN] [1,2,0,4,NaN] [1,2,0,4,NaN]
[NaN,2,0,4,NaN] [1,2,5,5,NaN] [7,2,0,4,NaN] [1,2,0,4,NaN] [1,5,0,4,NaN]
[1,2,NaN,4,NaN] [7,2,0,4,NaN] [1,5,0,4,NaN] [1,2,0,4,NaN] [5,2,0,4,NaN]
[1,7,5,5,NaN] [1,NaN,0,4,NaN] [1,2,0,4,NaN] [1,NaN,0,4,NaN] [1,2,7,4,NaN]
[1,2,0,4,NaN] [NaN,2,0,4,NaN] [NaN,2,NaN,4,NaN] [7,7,0,4,NaN] [1,5,0,4,NaN]

(okay it didn't format correctly but i guess you could get a rough idea)

From looking at answers to similar questions online i've attempted the following solutions:
x(isnan([x{:}]))={0}
and
x(cellfun(@isnan,x))={0}

but neither seem to do what i want.

I'd really appreciate any help,

Thanks, Cat

Subject: Replace NaN in cell array with zero

From: James Tursa

Date: 10 Sep, 2010 15:02:22

Message: 2 of 6

"Cat " <c.o'connor@vet.gla.ac.uk> wrote in message <i6dcfq$n8n$1@fred.mathworks.com>...
> Hello everybody,
>
> I'm having a wee problem trying to figure out how to replace all NaN's in my cell array with a value of zero.
>
> A simplified version of my array looks something like this:
> x=
> [1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN] [1,2,0,4,NaN] [1,2,0,4,NaN]
> [NaN,2,0,4,NaN] [1,2,5,5,NaN] [7,2,0,4,NaN] [1,2,0,4,NaN] [1,5,0,4,NaN]
> [1,2,NaN,4,NaN] [7,2,0,4,NaN] [1,5,0,4,NaN] [1,2,0,4,NaN] [5,2,0,4,NaN]
> [1,7,5,5,NaN] [1,NaN,0,4,NaN] [1,2,0,4,NaN] [1,NaN,0,4,NaN] [1,2,7,4,NaN]
> [1,2,0,4,NaN] [NaN,2,0,4,NaN] [NaN,2,NaN,4,NaN] [7,7,0,4,NaN] [1,5,0,4,NaN]
>
> (okay it didn't format correctly but i guess you could get a rough idea)
>
> From looking at answers to similar questions online i've attempted the following solutions:
> x(isnan([x{:}]))={0}
> and
> x(cellfun(@isnan,x))={0}
>
> but neither seem to do what i want.

You might define your own function for this and use cellfun with it. e.g.,

function x = nanzero(x)
x(isnan(x)) = 0;
return
end

cellfun(@nanzero,x,'UniformOutput',false)


James Tursa

Subject: Replace NaN in cell array with zero

From: Matt Fig

Date: 10 Sep, 2010 15:09:05

Message: 3 of 6

Another approach:

% Data
x = {[1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN]}

% Engine
B = cellfun(@isnan,x,'Un',0);
for ii = 1:length(x),x{ii}(B{ii}) = 0;end

Subject: Replace NaN in cell array with zero

From: Oleg Komarov

Date: 10 Sep, 2010 15:20:23

Message: 4 of 6

Another solution may be:

In = cell2mat(In(:));
In(isnan(In)) = 0;
In = reshape(num2cell(In,2),5,5);

Oleg

Subject: Replace NaN in cell array with zero

From: Matt Fig

Date: 10 Sep, 2010 15:40:21

Message: 5 of 6

"Matt Fig" <spamanon@yahoo.com> wrote in message <i6dhmh$dkf$1@fred.mathworks.com>...
> Another approach:
>
> % Data
> x = {[1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN]}
>
> % Engine
> B = cellfun(@isnan,x,'Un',0);
> for ii = 1:length(x),x{ii}(B{ii}) = 0;end

Even simpler:

% Data
x = {[1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN]}

% Engine
for ii = 1:length(x),x{ii}(isnan(x{ii})) = 0;end

Subject: Replace NaN in cell array with zero

From: Thomas Vanaret

Date: 13 Sep, 2010 08:27:25

Message: 6 of 6

I know I'm a bit late, but after (some) reflexion, I've found a one line solution without any loop using nansum (Statistical Toolbox - sorry)

% input
x = {[1,2,0,4,NaN] [1,2,3,NaN,NaN] [1,5,7,4,NaN]; [NaN,2,0,4,NaN] [1,2,5,5,NaN] [7,2,0,4,NaN]} % 2 x 3 cell array

% solution
y = cellfun(@(M) nansum(M,1), x, 'UniformOutput', false)


% Explanation : nansum(NaN) return 0

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us