Akzeptierte Antwort

Tim
Tim am 22 Jun. 2015
Bearbeitet: Tim am 22 Jun. 2015

15 Stimmen

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;

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 22 Jun. 2015
Bearbeitet: Azzi Abdelmalek am 22 Jun. 2015

2 Stimmen

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 am 22 Jun. 2015

1 Stimme

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 Kommentare

Amir Mohammad Babaie Parsa
Bearbeitet: Amir Mohammad Babaie Parsa am 25 Mai 2022
Hi
It really helped me, Thanks a zillion.
Rafiqul Islam
Rafiqul Islam am 31 Okt. 2022
Walter Roberson Thanks a lot

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by