Thread Closed

Matlab Question: variables that depend on previous variables

 
Share Thread Thread Tools
Jun8-10, 08:19 PM   #1
 

Matlab Question: variables that depend on previous variables


Hi,

I was wondering how you can get this to work in MatLab:

input is n

for i from 1 to n
x0 = 1
xi = x(i-1) + 2


So that for i = 1, x1 = x0 +2, or x1 = 1 + 2

Is there anyway to express this in MatLab?

Thanks!
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> 'Whodunnit' of Irish potato famine solved
>> The mammoth's lament: Study shows how cosmic impact sparked devastating climate change
>> Curiosity Mars rover drills second rock target
Jun8-10, 10:32 PM   #2
 
A quick and dirty way to do it is as follows.

Code:
n = 10;
x = [1 zeros(1, n)];

for i = 2:n+1
   x(i) = x(i-1) + 2;
end
This is, however, bad practice since it takes n loops to achieve what you want; it's not the sort of thing you'd use in serious code since you should generally avoid for() loops in Matlab wherever possible.

A much better solution would probably involve some use of the filter command. Check the docs for more info.
 
Jun9-10, 01:16 AM   #3
 
Mentor
Quote by shoehorn View Post
This is, however, bad practice since it takes n loops to achieve what you want; it's not the sort of thing you'd use in serious code since you should generally avoid for() loops in Matlab wherever possible.

A much better solution would probably involve some use of the filter command. Check the docs for more info.
I don't buy this at all. As fast as CPUs are these days, even interpreted languages (which I believe Matlab to be) are able to execute thousands of iterations in very little time. If there's any bottleneck, it's likely Matlab is waiting on I/O.
 
Jun9-10, 01:57 AM   #4
 

Matlab Question: variables that depend on previous variables


Quote by Mark44 View Post
I don't buy this at all. As fast as CPUs are these days, even interpreted languages (which I believe Matlab to be) are able to execute thousands of iterations in very little time. If there's any bottleneck, it's likely Matlab is waiting on I/O.
I use Matlab everyday at my job and avoiding loops can make a huge difference. I also find it quite fun to try and vectorize various algorithms. Granted, for scripts that only take a second to run, its all a wash.
 
Jun9-10, 02:00 AM   #5
 
n = 10;
x = 1:2:(2*n+1);
 
Jun9-10, 09:56 AM   #6
 
Quote by Mark44 View Post
I don't buy this at all. As fast as CPUs are these days, even interpreted languages (which I believe Matlab to be) are able to execute thousands of iterations in very little time. If there's any bottleneck, it's likely Matlab is waiting on I/O.
Anyone who has used Matlab for any serious work knows that the cardinal rule is to avoid loops whenever possible. This is not debatable in any sense.

Indeed, as matonski said, avoiding loops and vectorising your code can see huge gains in performance, particularly on multiple cores.

Quote by matonski
n = 10;
x = 1:2:(2*n+1);
Indeed. That's much better than a loop.
 
Thread Closed
Thread Tools


Similar Threads for: Matlab Question: variables that depend on previous variables
Thread Forum Replies
MATLAB vector variables Math & Science Software 6
Passing variables in MATLAB Engineering, Comp Sci, & Technology Homework 2
solving for variables in matlab Engineering, Comp Sci, & Technology Homework 1
Complex conjugate variables as independent variables in polynomial equations Linear & Abstract Algebra 0
representing a function of three variables in matlab Math & Science Software 0