Azzera filtri
Azzera filtri

Selecting/counting NaN elements

14 visualizzazioni (ultimi 30 giorni)
Jasmine Karim
Jasmine Karim il 17 Set 2018
Commentato: Stephen23 il 19 Set 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 Commenti
Stephen23
Stephen23 il 17 Set 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 il 18 Set 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).

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 19 Set 2018
Modificato: Stephen23 il 19 Set 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 Commenti
Jasmine Karim
Jasmine Karim il 19 Set 2018
Modificato: Jasmine Karim il 19 Set 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 il 19 Set 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by