Matlab Question: variables that depend on previous variables

In summary, the conversation is about finding a way to express a given function in MatLab. The first suggestion is to use a for loop, but it is noted that this is not considered good practice. A better solution would involve the use of the filter command, which is recommended to be checked in the documentation. Another person in the conversation disagrees with the statement about avoiding loops and suggests using vectorization for better performance. Finally, a solution using vectorization is provided as an alternative to the for loop.
  • #1
planar
1
0
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!
 
Physics news on Phys.org
  • #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.
 
  • #3
shoehorn said:
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.
 
  • #4
Mark44 said:
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.
 
  • #5
n = 10;
x = 1:2:(2*n+1);
 
  • #6
Mark44 said:
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.

matonski said:
n = 10;
x = 1:2:(2*n+1);

Indeed. That's much better than a loop.
 

1. What is the syntax for creating variables that depend on previous variables in Matlab?

The syntax for creating variables that depend on previous variables in Matlab is: newVariable = previousVariable + 5;

2. How do I access the value of a previous variable in Matlab?

You can access the value of a previous variable in Matlab by using its variable name. For example, if your previous variable is called "x", you can access its value by typing "x" in your code.

3. Can I use conditional statements to create variables that depend on previous variables in Matlab?

Yes, you can use conditional statements such as "if" and "else" to create variables that depend on previous variables in Matlab. For example: if previousVariable > 10, newVariable = previousVariable + 5;

4. Is it possible to create a loop that creates variables that depend on previous variables in Matlab?

Yes, you can create a loop that creates variables that depend on previous variables in Matlab. Make sure to initialize the previous variable outside of the loop and update its value within the loop.

5. Can I use functions to create variables that depend on previous variables in Matlab?

Yes, you can use functions to create variables that depend on previous variables in Matlab. Simply pass the previous variable as an argument to the function and use it to calculate the value of the new variable.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top