채택된 답변

Tim
Tim 2015년 6월 22일
편집: Tim 2015년 6월 22일

15 개 추천

A more polite way to ask in the event of urgency would be something like:
"I really need help on this one, time is a factor!"
a =[2:6 17:22]
for i=1:length(a)
y=2*a(i)
end
Or if you want to do it without a loop use vector form, which is generally more efficient in MATLAB:
a =[2:6 17:22];
y=2.*a;

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 22일
편집: Azzi Abdelmalek 2015년 6월 22일

2 개 추천

It's better to avoid the variable i, because it's used by Matlab with complex numbers.
for ii= [2:6,17:22]
Y= 2*ii;
end
Walter Roberson
Walter Roberson 2015년 6월 22일

1 개 추천

for i = [2:6,17:22]
Y = 2*i;
end
Note that you are overwriting Y completely each time, which is a common error in for loops. When you are looping over values that are not consecutive integers and you want to get one output value for each input value then use something like
ivals = [2:6,17:22];
for k = 1 : length(ivals);
i = ivals(k);
Y(k) = 2 * i;
end

댓글 수: 2

Amir Mohammad Babaie Parsa
Amir Mohammad Babaie Parsa 2022년 5월 25일
편집: Amir Mohammad Babaie Parsa 2022년 5월 25일
Hi
It really helped me, Thanks a zillion.
Rafiqul Islam
Rafiqul Islam 2022년 10월 31일
Walter Roberson Thanks a lot

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

카테고리

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

태그

질문:

2015년 6월 22일

댓글:

2022년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by