Cumulative sum of a number to create an array
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Adam Danz
le 24 Sep 2019
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
le 24 Sep 2019
Modifié(e) : John D'Errico
le 24 Sep 2019
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. :)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!