开发者

Common way to generate finite geometric series in MATLAB

Suppose I have some number a, and I want to get vector [ 1 , a , a^2 , ... , a^N ]. I use [ 1 , cump开发者_运维知识库rod( a * ones( 1 , N - 1 ) ) ] code. What is the best (and propably efficient) way to do it?


What about a.^[0:N] ?


ThibThib's answer is absolutely correct, but it doesn't generalize very easily if a happens to 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 could also utilize the built-in function vander (although the order is different, but that's easily fixed if needed), to produce:

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

And with vector valued a:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜