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

 Risposta accettata

David Hill
David Hill il 9 Nov 2021
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 Commento

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by