How can I convert while loop to for loop?

2 Ansichten (letzte 30 Tage)
John
John am 5 Jan. 2022
Beantwortet: Voss am 5 Jan. 2022
This is the loop
while i<=5
E_(i)= x(i).^2;
if i==1
disp(['The amount of energy at fundamental harmonic is: ' num2str(E_(i))]);
else
disp(['The amount of energy at harmonic ' num2str(i-1) ' is: ' num2str(E_(i))]);
end
i=i+1;
end

Akzeptierte Antwort

Voss
Voss am 5 Jan. 2022
Assuming the while loop starts with i = 1:
for i = 1:5
E_(i)= x(i).^2;
if i==1
disp(['The amount of energy at fundamental harmonic is: ' num2str(E_(i))]);
else
disp(['The amount of energy at harmonic ' num2str(i-1) ' is: ' num2str(E_(i))]);
end
end

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