| 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! |
| 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 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
|
|
| Jun9-10, 01:57 AM | #4 |
|
|
Matlab Question: variables that depend on previous variables |
| Jun9-10, 02:00 AM | #5 |
|
|
n = 10;
x = 1:2:(2*n+1); |
| Jun9-10, 09:56 AM | #6 |
|
|
Indeed, as matonski said, avoiding loops and vectorising your code can see huge gains in performance, particularly on multiple cores. |
| 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 | ||