필터 지우기
필터 지우기

How to put a variable equal to a vector in an if cycle

조회 수: 2 (최근 30일)
AlexDp
AlexDp 2019년 9월 16일
댓글: AlexDp 2019년 9월 17일
Hi all,
i'm trying to to find a way to have this example:
for t=2:365
TIMEFAILURECOG= [2 4 10 50];
Matrixdiff=[0,TIMEFAILURECOG];
DIFFERENCE=diff(Matrixdiff);
INDEXCOG1=find(DIFFERENCE>2);
INDEXCOG2=find(DIFFERENCE<=2);
DD=TIMEFAILURECOG(1,INDEXCOG1);
BB=TIMEFAILURECOG(1,INDEXCOG2);
if t==TIMEFAILURE(1,INDEXCOG1) % for every t equal to each value of TIMEFAILURECOG(1,INDEXCOG1)
y=6;
elseif t==TIMEFAILURECOG(1,INDEXCOG2) % for every t equal to each value of TIMEFAILURECOG(1,INDEXCOG2)
y=4;
else
y=0;
end
end
Thanks to anyone who can help me!

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 9월 17일
편집: Fabio Freschi 2019년 9월 17일
My guess
% your data
TIMEFAILURECOG= [2 4 10 50];
Matrixdiff=[0,TIMEFAILURECOG];
DIFFERENCE=diff(Matrixdiff);
INDEXCOG1=find(DIFFERENCE>2);
INDEXCOG2=find(DIFFERENCE<=2);
DD=TIMEFAILURECOG(1,INDEXCOG1);
BB=TIMEFAILURECOG(1,INDEXCOG2);
% set t as a vector
t = 2:365;
% preallocate y as long as t
y = zeros(size(t));
% set y values according to t
y(ismember(t,DD)) = 6;
y(ismember(t,BB)) = 4;
  댓글 수: 1
AlexDp
AlexDp 2019년 9월 17일
Thank you very much Fabio! It's a great solution.

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

추가 답변 (0개)

카테고리

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