Common way to generate finite geometric series in MATLAB

前端 未结 2 1165
攒了一身酷
攒了一身酷 2021-01-18 02:06

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 ) ) ] cod

相关标签:
2条回答
  • 2021-01-18 02:30

    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 讨论(0)
  • 2021-01-18 02:40

    What about a.^[0:N] ?

    0 讨论(0)
提交回复
热议问题