are the first 3 elements of a vector NaN?

조회 수: 1 (최근 30일)
Sabbas
Sabbas 2012년 8월 11일
Dear all,I have
A={
[NaN]
[NaN]
[NaN]
[3]
[3]
[6]
[4]}
I want to find a rule that will tell me if the first 3 elements of a vector A are NaN or not
thanks
  댓글 수: 2
Yash
Yash 2012년 8월 11일
you can apply condition on them
Sabbas
Sabbas 2012년 8월 11일
coul;d you please be more specific?

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

채택된 답변

Matt Fig
Matt Fig 2012년 8월 11일
A is a cell.
all(cellfun(@isnan,A(1:3)))

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 8월 11일
편집: Image Analyst 2012년 8월 11일
Method 1. Inspects the first three cells ONLY.
A = {
[NaN]
[NaN]
[NaN]
[3]
[3]
[6]
[4]}
if isnan(A{1}) && isnan(A{2}) && isnan(A{3})
uiwait(msgbox('The first three cells are nans'));
else
uiwait(msgbox('The are not nans.'));
end
Method 2. Somewhat more robust and flexible.
% Define starting and ending cell to inspect.
firstElementToCheck = 1;
lastElementToCheck = 3;
nanLocations = isnan([A{:}])
if sum(nanLocations(firstElementToCheck:lastElementToCheck)) == (lastElementToCheck - firstElementToCheck) + 1
message = sprintf('The cells between %d and %d are all NaNs', ...
firstElementToCheck, lastElementToCheck);
uiwait(msgbox(message));
else
message = sprintf('The cells between %d and %d are NOT ALL NaNs', ...
firstElementToCheck, lastElementToCheck);
uiwait(msgbox(message));
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by