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

4 visualizaciones (últimos 30 días)
AlexDp
AlexDp el 16 de Sept. de 2019
Comentada: AlexDp el 17 de Sept. de 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!

Respuesta aceptada

Fabio Freschi
Fabio Freschi el 17 de Sept. de 2019
Editada: Fabio Freschi el 17 de Sept. de 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;

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by