General way to generate finite geometric series in MATLAB

Suppose I have some number a , and I want to get the vector [ 1 , a , a^2 , ... , a^N ] . I am using the code [ 1 , cumprod( a * ones( 1 , N - 1 ) ) ] . What is the best (and possibly effective) way to do this?

+7
source share
2 answers

What about a.^[0:N] ?

+13
source

ThibThib's answer is absolutely correct, but it is not very generalized if a occurs with a vector. So, as a starting point:

 > a= 2 a = 2 > n= 3 n = 3 > a.^[0: n] ans = 1 2 4 8 

Now you can also use the built-in vander function (although the order is different, but it is easily fixed if necessary) to create:

 > vander(a, n+ 1) ans = 8 4 2 1 

And with the vector value a :

 > a= [2; 3; 4]; > vander(a, n+ 1) ans = 8 4 2 1 27 9 3 1 64 16 4 1 
+2
source

All Articles