필터 지우기
필터 지우기

Selecting/counting NaN elements

조회 수: 14 (최근 30일)
Jasmine Karim
Jasmine Karim 2018년 9월 17일
댓글: Stephen23 2018년 9월 19일
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
Stephen23
Stephen23 2018년 9월 17일
@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 2018년 9월 18일
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).

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2018년 9월 19일
편집: Stephen23 2018년 9월 19일
"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
Jasmine Karim
Jasmine Karim 2018년 9월 19일
편집: Jasmine Karim 2018년 9월 19일
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 2018년 9월 19일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by