Matlab Question: variables that depend on previous variables

  • Context: MATLAB 
  • Thread starter Thread starter planar
  • Start date Start date
  • Tags Tags
    Matlab Variables
Click For Summary

Discussion Overview

The discussion revolves around implementing a specific iterative calculation in MATLAB, where each variable depends on the previous one. Participants explore different coding approaches, performance considerations, and best practices in MATLAB programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about how to express a loop in MATLAB to calculate a sequence based on previous values.
  • Another participant provides a basic solution using a for loop but cautions that this approach is considered bad practice due to performance concerns.
  • Some participants argue against the notion that for loops are inherently inefficient, suggesting that modern CPUs can handle many iterations quickly and that performance issues may arise from I/O operations instead.
  • A participant emphasizes the importance of avoiding loops in MATLAB for serious work and suggests that vectorization can lead to significant performance improvements.
  • A more concise solution using MATLAB's vectorization capabilities is presented, which avoids loops altogether.

Areas of Agreement / Disagreement

There is disagreement regarding the efficiency of for loops in MATLAB. Some participants advocate for avoiding loops in favor of vectorized solutions, while others argue that the performance impact of loops may be overstated.

Contextual Notes

Participants express varying opinions on the performance implications of using loops versus vectorization, indicating that the discussion is influenced by personal experiences and specific use cases in MATLAB.

planar
Messages
1
Reaction score
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
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.
 
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.
 
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.
 
n = 10;
x = 1:2:(2*n+1);
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K