# matlab for loops?

• Feb 27th 2010, 05:53 PM
Mathhelpz
matlab for loops?
http://img685.imageshack.us/img685/3025/39242128.png

So far i have this

VEC = input('Please type a value that is > 9 and < 21:')
if VEC < 10 || VEC > 21
error('Incorrect Input... Quitting!')
Vec1 = input('Type in a value for the first element:')

But not sure if I'm on the right track.. Could someone help me with this problem
• Feb 28th 2010, 08:11 AM
CaptainBlack
Quote:

Originally Posted by Mathhelpz
http://img685.imageshack.us/img685/3025/39242128.png

So far i have this

VEC = input('Please type a value that is > 9 and < 21:')
if VEC < 10 || VEC > 21
error('Incorrect Input... Quitting!')
Vec1 = input('Type in a value for the first element:')

But not sure if I'm on the right track.. Could someone help me with this problem

Have you tested it?

CB
• Feb 28th 2010, 08:56 AM
Mathhelpz
yea,

this is my script so far

% initialize the vector
VEC = [];
n = 1;

VEC_length = input('Please type a value that is > 9 and < 21:');
if VEC_length < 10 || VEC_length > 21
error('Incorrect Input... Quitting!')
end

Vec1 = input('Type in a value for the first element:')

% Add first element to the vector
VEC(n)=Vec1;

% Use a for loop to add more number to the vector
for i=1:VEC_length
n=n+1;
VEC(n)= Vec1 +i;
end

I don't really know what to do on step 4? COuld you help me
• Feb 28th 2010, 11:19 AM
CaptainBlack
Quote:

Originally Posted by Mathhelpz
yea,

this is my script so far

% initialize the vector
VEC = [];
n = 1;

VEC_length = input('Please type a value that is > 9 and < 21:');
if VEC_length < 10 || VEC_length > 21
error('Incorrect Input... Quitting!')
end

Vec1 = input('Type in a value for the first element:')

% Add first element to the vector
VEC(n)=Vec1;

% Use a for loop to add more number to the vector
for i=1:VEC_length
n=n+1;
VEC(n)= Vec1 +i;
end

I don't really know what to do on step 4? COuld you help me

This is wrong:

Code:

```VEC_length = input('Please type a value that is > 9 and < 21:'); if VEC_length < 10 || VEC_length > 21   error('Incorrect Input... Quitting!') end```
Try inputting a value of 21

CB
• Feb 28th 2010, 11:23 AM
CaptainBlack
Quote:

Originally Posted by Mathhelpz

I don't really know what to do on step 4? COuld you help me

Code:

```if VEC(1)>=25   VEC(1)=0; end for k=2:VEC_LENGTH   VEC(k)=VEC(k-1)+1;   if VEC(k)>=25     VEC(k)=0;   end end```
CB
• Feb 28th 2010, 09:49 PM
Mathhelpz
thanks for that, i almost finised it =D