1

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.

1 Answer 1

2

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.

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

3 Comments

beautiful one liners
Indeed, very nice solution. Thanks a lot :-)
Glad If it can help!

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.