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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2019년 1월 22일

1 개 추천

move the last line inside the for-loop.

댓글 수: 3

Jake
Jake 2019년 1월 22일
Thank you!
Fangjun Jiang
Fangjun Jiang 2019년 1월 22일
And add temp_si=zeros(1,101) before the for-loop to pre-allocate memory.
Jan
Jan 2019년 1월 22일
편집: Jan 2019년 1월 22일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2019년 1월 22일

편집:

Jan
2019년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by