Borrar filtros
Borrar filtros

Selecting/counting NaN elements

14 visualizaciones (últimos 30 días)
Jasmine Karim
Jasmine Karim el 17 de Sept. de 2018
Comentada: Stephen23 el 19 de Sept. de 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 comentarios
Stephen23
Stephen23 el 17 de Sept. de 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 el 18 de Sept. de 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).

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 19 de Sept. de 2018
Editada: Stephen23 el 19 de Sept. de 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 comentarios
Jasmine Karim
Jasmine Karim el 19 de Sept. de 2018
Editada: Jasmine Karim el 19 de Sept. de 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 el 19 de Sept. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by