Andi
Andi

Reputation: 4899

Index of scalar NaNs in cell array

Consider the following cell array:

test = cell(2,2);
test(1,:) = {NaN};
test{2,1} = [1,2,3];
test{2,2} = [4,NaN,6];

I would like to identify those cells which directly consist of an NaN scalar. I tried isnan in conjunction with cellfun, however, that identifies all NaNs within a vector as well.

nanIdx = cellfun(@isnan, test, 'UniformOutput', false)

As a result I am looking for nanIdx = [true,true ; false,false] of type logical.

Upvotes: 1

Views: 51

Answers (1)

rahnema1
rahnema1

Reputation: 15867

You can define the anonymous function as @(x)isscalar(x) && isnan(x):

nanIdx = cellfun(@(x)isscalar(x) && isnan(x), test)

More conditions can be provided using any of is* functions.

Upvotes: 2

Related Questions