Vector where each value is X times larger than the last

28 次查看(过去 30 天)
I'm trying to recreate some log scale plots reported in this paper, the method they use calls for a vector of bin edges such that each one is 3.1% larger than the previous one.
I can do this quite easily using a while loop:
s1 = 1; % minimum bin value
s2 = 1000; % maximum bin value we would like
m = 1.031; % scaling factor, each bin will be m*the last one in size
xi = s1; % starting point
while 1
xi(end+1) = xi(end)*m; % new bin is the last one*m
if xi(end)>s2 % when we exceed value s2, stop
break
end
end
This code obviously has some serious drawbacks, but it is also just very ugly.
I was wondering if someone could think of a more elegant and efficient approach?
  2 个评论
Stephen23
Stephen23 2024-12-16,19:09
For those interested to know what that loop produces:
s1 = 1; % minimum bin value
s2 = 1000; % maximum bin value we would like
m = 1.031; % scaling factor, each bin will be m*the last one in size
xi = s1; % starting point
while 1
xi(end+1) = xi(end)*m; % new bin is the last one*m
if xi(end)>s2 % when we exceed value s2, stop
break
end
end
display(xi)
xi = 1×228
1.0000 1.0310 1.0630 1.0959 1.1299 1.1649 1.2010 1.2383 1.2766 1.3162 1.3570 1.3991 1.4425 1.4872 1.5333 1.5808 1.6298 1.6803 1.7324 1.7861 1.8415 1.8986 1.9574 2.0181 2.0807 2.1452 2.2117 2.2803 2.3509 2.4238
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Just for fun, without CUMPROD:
s1 * m.^(0:ceil(log(s2/s1)/log(m)))
ans = 1×228
1.0000 1.0310 1.0630 1.0959 1.1299 1.1649 1.2010 1.2383 1.2766 1.3162 1.3570 1.3991 1.4425 1.4872 1.5333 1.5808 1.6298 1.6803 1.7324 1.7861 1.8415 1.8986 1.9574 2.0181 2.0807 2.1452 2.2117 2.2803 2.3509 2.4238
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Neuroesc
Neuroesc 2024-12-17,11:25
I will use cumprod because I prefer to use in-built functions where possible, but thank you for this answer, it helped me a lot to understand why this works. I'm sure it will also be very useful to people who don't want to or can't use cumprod.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2024-12-16,18:33
Use cumprod.
x = [2 3*ones(1, 4)]
x = 1×5
2 3 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = cumprod(x)
y = 1×5
2 6 18 54 162
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you don't know how many elements you want y to have, just what you want the largest value to be, solve the equation (multiplication factor)^n = (upper bound) [or M^n = U] by taking the log of both sides and dividing.
U = 2000;
M = 3;
n = ceil(log(U)./log(M));
x = [1 M*ones(1, n)];
y = cumprod(x)
y = 1×8
1 3 9 27 81 243 729 2187
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note that since I used ceil the largest value in y is actually greater than U. If I wanted to stop just before the largest value in y would be greater than U I'd have used floor instead.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by