How to put a variable equal to a vector in an if cycle
조회 수: 2 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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;
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!