Use linspace without scalar input?
Show older comments
I want to use linspace that goes from two non-scalar terms. Is there a way to use linspace with specified number of points, n, for decimal values for inputs? Example code is below for what I am looking for.
x = 0.01;
y = [1.01, 3.01];
n = 10;
dxy = linspace(x, y, n);
3 Comments
the cyclist
on 29 Aug 2017
Rather than telling us an input syntax that clearly doesn't work, it might be better to tell us what output you are hoping to get.
It seems like you are trying to over-specify what you want to get.
KSSV
on 29 Aug 2017
n cannot be a decimal.....it should be an integer.
Andrew Poissant
on 29 Aug 2017
Accepted Answer
More Answers (2)
x = 0.01;
y = [1.01, 3.01];
n = 10;
dxy = [linspace(x, y(1), n); ...
linspace(x, y(2), n)];
Or:
Step = repmat((y(:) - x(:)) / (n - 1), 1, n);
Step(:, 1) = x(:);
dxy = cumsum(Step);
1 Comment
Andrew Poissant
on 29 Aug 2017
>> x = 0.01;
>> y = [1.01, 3.01];
>> n = 10;
>> bsxfun(@plus,x*linspace(1,0,n),bsxfun(@times,y(:),linspace(0,1,n)))
ans =
0.010000 0.121111 0.232222 0.343333 0.454444 0.565556 0.676667 0.787778 0.898889 1.010000
0.010000 0.343333 0.676667 1.010000 1.343333 1.676667 2.010000 2.343333 2.676667 3.010000
MATLAB versions with implicit expansion could probably do this (untested):
x.*linspace(1,0,n) + y(:).*linspace(0,1,n)
1 Comment
Andrew Poissant
on 29 Aug 2017
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!