Cumulative Sum of a Vector - Syntax

前端 未结 3 1373
夕颜
夕颜 2021-01-25 08:42

I am trying to resolve why the following Matlab syntax does not work.

  1. I have an array A = [2 3 4 5 8 9...]
  2. I wish to create an indexed cumulative, for
3条回答
  •  余生分开走
    2021-01-25 09:45

    For calculating the cumulative sum, you should be using cumsum:

    >> A = [2 3 4 5 8 9]
    
    A =
    
         2     3     4     5     8     9
    
    >> cumsum(A)
    
    ans =
    
         2     5     9    14    22    31
    

    The issue is that 1:x is 1 and that sum reduces linear arrays. To do this properly, you need a 2d array and then sum the rows:

    s(x)=sum(triu(repmat(A,[prod(size(A)) 1])'))
    

提交回复
热议问题