Hello everyone!
I have a problem but I can solve it. I would like to create a new vector with using if statement in a for loop. The problem is I dont' know how to call the new vector last member in the if statement. I know the first member the new vector. The first member is xn1.
I would like that if the if statement is true than add fn the last member the new vector. If false the new member is equal the last member.
I hope somebody can help me. Thanks a lot!
xn1=0.6;
xs=0.9:0.3:30;
fn=0.5;
fs=0.3;
r1=0.8;
r2=0.4;
xn=[];
for i=1:lenght(xs)
if (xs(i)+fs-r2)>=(xn(i-1)+fn/2)
% if true xn last member + fn
xn(i)=xn(i-1)+fn
else
% if false xn not change
xn(i)=xn(i-1)
end
end

 Respuesta aceptada

David Hill
David Hill el 9 de Nov. de 2021

0 votos

xn1=0.6;
xs=0.9:0.3:30;
fn=0.5;
fs=0.3;
r1=0.8;
r2=0.4;
xn=0;
for i=1:lenght(xs)
if (xs(i)+fs-re2)>=(xn(i)+fn/2)%have no idea what re2 is
xn(i+1)=xn(i)+fn;%cannot index xn(0)
else
xn(i+1)=xn(i);
end
end

1 comentario

Zsolt Illés
Zsolt Illés el 9 de Nov. de 2021
I corrected it re2 is r2. Thanks a lot. Now it's working

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 9 de Nov. de 2021

Comentada:

el 9 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by