ronica
ronica

Reputation: 1

Matlab for loop that manipulates a list?

I am new to Matlab so I apologize for relatively easy questions.

I have:

for i=0:10
    values(:,1) = (2.*i-20)*5.;
end

I want the script to produce a vector of 11 values that have been changed by the (2.*i-20)*5. for each i.

Upvotes: 0

Views: 57

Answers (1)

Autonomous
Autonomous

Reputation: 9075

I think you meant to do this:

for i=0:10
    values(i+1,1) = (2.*i-20)*5.; %you can also write -> values(i)
end

More general way to fill an array in a for loop when your loop variable doesn't go from 1 to the desired value:

count=0;
for i=0:10
    count=count+1;
    values(count,1) = (2.*i-20)*5.;
end

But wait, this is not at all a good way of programming in MATLAB. You should do such operations as follows:

i=0:10;
values=(2.*i-20)*5.;  

The above solution is called a vectorized solution.

Upvotes: 1

Related Questions