Cumulative sum of a number to create an array

조회 수: 1 (최근 30일)
Isabelle Bester
Isabelle Bester 2019년 9월 24일
편집: John D'Errico 2019년 9월 24일
Hello!
I have a number (1182.225) and I am trying to create an array from this number that goes as follows:
Array = [1182.225, 1182.225*2, 1182.225*3, ..... 1182.225*24]
So that the array contains the number multiplied by 1 through 24 and each product is placed into the array. How can I write this in order to not type each multiplication manually?
Thanks for any feedback!
  댓글 수: 2
Adam Danz
Adam Danz 2019년 9월 24일
Matt J's answer does what you're describing but note that it's not a cumulative sum (as mentioned in your question title).
John D'Errico
John D'Errico 2019년 9월 24일
편집: John D'Errico 2019년 9월 24일
Why does this feel somehow too silly to post as an answer to me?
1182.225*cumsum(ones(1,24))
Like using a Mack truck, to carry a pea to Boston. Well, not quite as bad.
Ok, perhaps this?
cumsum(1182.225)*(1:24)
Ok, I'm getting the hang of it now.
cumsum(1182.225)*diff((1:25).^2 - (1:25))/2
That seems to be closer to my target. :)
Hmm. Lets see, the roots of a one of the Chebyshef family of polynomials are equally spaced. I could construct the proper member, in symbolic form, then use solve to compute the roots. Or, I could construct a 24x24 matrix, with the property that one of the eigenvectors has the right shape. Easy enough. Then eig will do the work for me.
Nope. I like Matt's answer myself. Stick with that. :)

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2019년 9월 24일
편집: Matt J 2019년 9월 24일
Array=1182.225.*(1:24)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by