Entering a vector in a function with if else statement

조회 수: 2 (최근 30일)
Ida Lunde Hygum
Ida Lunde Hygum 2018년 3월 12일
댓글: KL 2018년 3월 12일
Hi Matlab community.
I made this function that should sort out data that differs more than +/- 20 % giving it "NaN" if it does. If the previous measurement varied more than 20 % it should should not do so, which i tried with the command: "(sorted_data(i-1)==NaN) && (i>1)" but when I use the debugger it does not enter this statement even though it is true. How come it won't enter this? Thanks for the help!
Kind Regards
function sorted_data=sort_for_outliers(data)
sorted_data=zeros(length(data),1);
for i=1:length(data)
if i==1
sorted_data(i)=[data(i)];
elseif (sorted_data(i-1)==NaN) && (i>1)
sorted_data(i)=[data(i)];
elseif data(i)>1.20.*data(i-1)
sorted_data(i)=[NaN];
elseif data(i)<0.80.*data(i-1)
sorted_data(i)=[NaN];
else
sorted_data(i)=[data(i)];
end
end
end
  댓글 수: 1
Torsten
Torsten 2018년 3월 12일
At the beginning of your function, you set "sorted_data" to zero. Thus sorted_data(i-1) will not be NaN.
Best wishes
Torsten.

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

답변 (1개)

KL
KL 2018년 3월 12일
편집: KL 2018년 3월 12일
I'm afraid your usage of sorted_data(i-1)==NaN would always return false. The right way to find if a value is nan is to use isnan
P.S: It's always good to pay attention to the warnings on the right hand vertical bar on your editor window.
  댓글 수: 2
Ida Lunde Hygum
Ida Lunde Hygum 2018년 3월 12일
Hi! I tried this and it worked, thank you!
KL
KL 2018년 3월 12일
You're welcome!

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by