Filter löschen
Filter löschen

Selecting/counting NaN elements

14 Ansichten (letzte 30 Tage)
Jasmine Karim
Jasmine Karim am 17 Sep. 2018
Kommentiert: Stephen23 am 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 Kommentare
Stephen23
Stephen23 am 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 am 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).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 19 Sep. 2018
Bearbeitet: Stephen23 am 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 Kommentare
Jasmine Karim
Jasmine Karim am 19 Sep. 2018
Bearbeitet: Jasmine Karim am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by