Selecting/counting NaN elements

14 views (last 30 days)
Jasmine Karim
Jasmine Karim on 17 Sep 2018
Commented: Stephen23 on 19 Sep 2018
I have a cell array where some values are NaN. The rest of the elements are characters. I want to count the NaN elements in addition to the characters, that I already have in place. Of course, it's not counting NaN values. I can turn NaN into characters or zeroes (the other elements are not numerical so the value wouldn't matter). However, if I try
isnan(A{a,b})
I get the error Undefined function 'isnan' for input arguments of type 'cell'.
  2 Comments
Stephen23
Stephen23 on 17 Sep 2018
@Jasmine Karim: it looks like you have multiply nested cell arrays. Please upload that variable in a .mat file, by clicking the paperclip button.
Jasmine Karim
Jasmine Karim on 18 Sep 2018
I edited my original question because I think it was incorrectly worded. I have a matrix that holds 6 matrices. In each of those matrices is a basic output that looks like A (attached here).

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 19 Sep 2018
Edited: Stephen23 on 19 Sep 2018
"I have a cell array where some values are NaN."
True. There are four NaN's in your cell array:
>> idx = cellfun(@(v)isnumeric(v)&&any(isnan(v)),A);
>> nnz(idx)
ans = 4
>> find(idx)
ans =
67
69
80
98
"The rest of the elements are characters"
False. In fact most of the cells contain numeric data:
>> numel(A)
ans = 132
>> nnz(cellfun(@isnumeric,A))
ans = 70
>> nnz(cellfun(@ischar,A))
ans = 62
Well, in any case, I showed you how to count the NaN's, as your question requested.
  2 Comments
Jasmine Karim
Jasmine Karim on 19 Sep 2018
Edited: Jasmine Karim on 19 Sep 2018
Thank you.
I wasn't clear, I meant that in the 3rd column, the rest of the elements are characters. Can I change the ones (in the 3rd column) that are NaN to characters as well?
The reason being that later on I filter the data in this matrix based on the 'yes' 'no' and the NaN responses actually signify an incorrect response.
Stephen23
Stephen23 on 19 Sep 2018
@Jasmine Karim: you can easily detect NaN values in the third column:
fun = @(v)isnumeric(v)&&isscalar(v)&&isnan(v);
idx = cellfun(fun,A(:,3))
A(idx,3) = {'hello world'}
If you only expect scalar NaN, then you might be able to simplify this to:
idx = cellfun(@isnan,A(:,3))
Experiment and see what works for your situation.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!