Hello, just looking for some quick help. How come when I execute this code, the only value for which .003 shows up is the 101st iteration of i?
for i = 1:101
if (i>=84)
a = .003;
elseif (i<84) && (i>=38)
a = 0;
elseif (i<38)
a = -.0065;
end
end
temp_si(i) = a

 Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 22 Jan 2019

1 vote

move the last line inside the for-loop.

3 commentaires

Jake
Jake le 22 Jan 2019
Thank you!
Fangjun Jiang
Fangjun Jiang le 22 Jan 2019
And add temp_si=zeros(1,101) before the for-loop to pre-allocate memory.
Jan
Jan le 22 Jan 2019
Modifié(e) : Jan le 22 Jan 2019
So either:
temp_si = zeros(1, 101);
for i = 1:101
if (i>=84)
a = .003;
elseif (i<84) && (i>=38)
a = 0;
elseif (i<38)
a = -.0065;
end
temp_si(i) = a
end
or:
temp_si = zeros(1, 101);
temp_si(1:37) = -0.0065;
temp_si(84:101) = 0.003;
Of course I prefer the latter.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by