Linspace with varying increment
조회 수: 157 (최근 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.
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!