Vector where each value is X times larger than the last
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Neuroesc
el 16 de Dic. de 2024 a las 18:05
Comentada: Neuroesc
el 17 de Dic. de 2024 a las 11:25
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 comentarios
Stephen23
el 16 de Dic. de 2024 a las 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)
Just for fun, without CUMPROD:
s1 * m.^(0:ceil(log(s2/s1)/log(m)))
Respuesta aceptada
Steven Lord
el 16 de Dic. de 2024 a las 18:33
Use cumprod.
x = [2 3*ones(1, 4)]
y = cumprod(x)
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)
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!