Info

This question is closed. Reopen it to edit or answer.

Function that creates an array of specified size from a first value where each next value is a multiple of the previous value?

1 view (last 30 days)
For example, if given 20 as first value, 0.75 as percent of first value and length or size 4 would produce:
[20,15,11.25,8.4375]
or essentially
[20,20*0.75,20*0.75^2,20*0.75^3]
for a more generalized version I was able solve this problem with a for loop, but I am curious if there is a built in function to accomplish this task?
  1 Comment
Stephen23
Stephen23 on 19 Jun 2015
The most "generalized version" in MATLAB is not using a loop, but using vectorized code like Azzi Abdelmalek has shown below. MATLAB is not a low-level language like C that relies on loops for everything: vectorized code is much more efficient in MATLAB!

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 18 Jun 2015
Edited: Azzi Abdelmalek on 18 Jun 2015
n=4
p=0.75
out=20*p.^(0:n-1)

Community Treasure Hunt

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

Start Hunting!