Linspace with varying increment

155 次查看(过去 30 天)
I'm interested in creating a set of numbers between fixed start and end points . I know I have 2 options to do this with a fixed step size:
s = 0; e = 1;
Num = s:0.1:e
Num = [0,0.1,0.2,0.3,...]
Num2 = linspace(s,e,5)
Num2 = [0,0.2500,0.5000,0.7500,1.0000]
However I'm interested in using a varying step size, for example:
s = 0.0625; e = 1;
Num3 = [0.0625,0.125, 0.25,0.5,1]
where the increment doubles at each step. I guess I'm seeking a method where given a start and end point and an array of increment sizes e.g
dx = 0.01
spacings = [dx,2*dx,3*dx,...]
I'm sure theres an elegant way to do this, any thoughts are greatly appreciated.

采纳的回答

Daniel M
Daniel M 2019-11-12
MATLAB provides functions that do basic things, like create linearly spaced vectors using the colon operator or linspace. It is up to the user to find ways to make these functions do the things we want.
n = -2:0.5:0;
x = 4.^n;
% ans =
% 0.0625 0.125 0.25 0.5 1

更多回答(1 个)

John D'Errico
John D'Errico 2019-11-12
编辑:John D'Errico 2019-11-12
The problem is, vendor provided software cannot work like that. You have descibed several cases of potential interest to you, from purely linear spacing, to a geometric spacing, to a spacing that starts out as linear, but then switches over to a different style of spacing, all perhaps with an end points of 0 and 1, but perhaps not always. And tomorrow, you will probably want something more creative yet. A general tool like linspace cannot be that flexible, nor can it read your mind. That is why, as Daniel said, you are the one who needs to be the supplier of intelligence.
Nothing stops you from writing code (a simple function) that allows you to specify a linear sequence up to some point, and then a geometric increasing one after that, to some arbitrary end point. Even If nobody else in the universe seems to care for that specific scheme, you can always create it - that is the beauty of a language like MATLAB. As well, your function need not be incredibly elegant, as long as it is sufficiently efficient that it does not create a bottleneck in your code, then who cares?
For example, suppose you wanted to create a function that takes three break points, and a number of points to generate in each segment? Then it would return a sequence that was linear in the first part, and increases geometrically in the second half of the interval?
function X = crazyspace(breaks,N1,N2)
% good documentation, that explains the meaning of breaks, N1, and N2
X = [linspace(breaks(1),breaks(2),N1),logspace(log10(breaks(2)),log10(breaks(3)),N2)];
X(N1+1) = [];
So we would expect...
X = crazyspace([0 .5 2],4,5)
X =
0 0.16667 0.33333 0.5 0.70711 1 1.4142 2
I'm sure I could have written it differently, but who cares? It would work. It is efficient.
Or, perhaps you could have some other spacing in the second half of the region, perhaps using cumsum. You are the driver here, as you should be.
  2 个评论
ADSW121365
ADSW121365 2019-11-13
I disagree with your initial statement entirely, indeed in other programming languages functions exist of the type: function(Start,End,Spacing(s)), where spacings can be entered as either a scalar (identical to the colon operator) or as an array of different spacings at each step, which was the reason I posed the question assuming MATLAB would have something similar.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by