Common way to generate finite geometric series in MATLAB

醉酒当歌 提交于 2019-12-30 18:47:13

问题


Suppose I have some number a, and I want to get vector [ 1 , a , a^2 , ... , a^N ]. I use [ 1 , cumprod( a * ones( 1 , N - 1 ) ) ] code. What is the best (and propably efficient) way to do it?


回答1:


What about a.^[0:N] ?




回答2:


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


来源:https://stackoverflow.com/questions/6307122/common-way-to-generate-finite-geometric-series-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!