How do I create a vector with a multiply sequence?
Ältere Kommentare anzeigen
Can I replace this loop?
vector(1)=100;
for i=2:10
vector(i)=vector(i-1)*0.7;
end
%%vector(10) will be 4.0354
I want to write the sequence , something like this:
vector=100:*0.7:4.0354
possible?
3 Kommentare
Sean de Wolski
am 12 Apr. 2012
+1, this is a well written question that can evoke a lot of good solutions with an opportunity for us to all learn.
Jan
am 12 Apr. 2012
Sean, you are right. +1
Sean de Wolski
am 12 Apr. 2012
How would you do this with filter?
Antworten (6)
Sean de Wolski
am 12 Apr. 2012
vector = 0.7.^(0:9)*100
And golf:
v=.7.^(0:9)*100
1 Kommentar
Andrei Bobrov
am 12 Apr. 2012
+1
Andrei Bobrov
am 12 Apr. 2012
vector = [1 cumprod(ones(1,9)*.7)]*100;
more variant
o10 = ones(10);
vector = 100*prod(o10 - .3*triu(o10,1));
Jan
am 12 Apr. 2012
vector = repmat(0.7, 1, 10);
vector(1) = 100;
vector = cumprod(vector);
Or as one-liner:
vector = cumprod([100, repmat(0.7, 1, 9)]);
Jan
am 12 Apr. 2012
An of course:
vector = [100, 70, 49, 34.3, 24.01, 16.807, 11.7649, ...
8.23543, 5.764801, 4.0353607];
This is fast, but not very flexible. :-)
1 Kommentar
Andrei Bobrov
am 12 Apr. 2012
+1, yes!
Honglei Chen
am 12 Apr. 2012
Just for fun
filter(1,[1 -0.7],[100 0 0 0 0 0 0 0 0 0])
1 Kommentar
Sean de Wolski
am 12 Apr. 2012
yay!
Honglei Chen
am 12 Apr. 2012
Another one
flipud(100*diag(vander(repmat(0.7,1,10))))
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!