Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
AlexDp
AlexDp am 16 Sep. 2019
Kommentiert: AlexDp am 17 Sep. 2019
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!

Akzeptierte Antwort

Fabio Freschi
Fabio Freschi am 17 Sep. 2019
Bearbeitet: Fabio Freschi am 17 Sep. 2019
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;

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by