フィルターのクリア

Use linspace without scalar input?

8 ビュー (過去 30 日間)
Andrew Poissant
Andrew Poissant 2017 年 8 月 29 日
コメント済み: Ali Talha Atici 2022 年 4 月 19 日
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 件のコメント
KSSV
KSSV 2017 年 8 月 29 日
n cannot be a decimal.....it should be an integer.
Andrew Poissant
Andrew Poissant 2017 年 8 月 29 日
My apologies. n will be an integer since it is the number of points. I want to use decimal values for x and y. So part of the output I am looking for would be dxy = [0.01 0.11 0.21 ... 1.01; 0.01 0.11 0.21 ... 3.01]. Hope this makes things clearer.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 8 月 29 日
編集済み: Guillaume 2017 年 8 月 29 日
x = 0.01;
y = [1.01, 3.01]; %would be better as a column vector as you wouldn't to tranpose it in the arrayfun
n = 10;
dxy = cell2mat(arrayfun(@(e) linspace(x, e, n), y', 'UniformOutput', false))
Or using a loop:
x = 0.01;
y = [1.01, 3.01]; %would be better as a column vector as you wouldn't to tranpose it in the arrayfun
n = 10;
dxy = zeros(numel(y), n);
for row = 1:numel(y)
dxy(row, :) = linspace(x, y(row), n);
end
or using Jan's idea but with R2016b or later syntax:
x = 0.01;
y = [1.01, 3.01]; %would be better as a column vector as you wouldn't the transpose in the calculation of dxy
n = 10;
dxy = (0:n-1) .* (y'-x)/n + x
  2 件のコメント
Andrew Poissant
Andrew Poissant 2017 年 8 月 29 日
Thank you! Your answer was very helpful
Ali Talha Atici
Ali Talha Atici 2022 年 4 月 19 日
Thank you, Guillaume,
I have a little update to the last script to match the results.
x = 0.01;
y = [1.01, 3.01]; %would be better as a column vector as you wouldn't the transpose in the calculation of dxy
n = 10;
dxy = (0:n-1) .* (y'-x)/(n-1) + x

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2017 年 8 月 29 日
編集済み: Jan 2017 年 8 月 29 日
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 件のコメント
Andrew Poissant
Andrew Poissant 2017 年 8 月 29 日
How would I generalize the first option if I have many values in the y vector? I tried the following:
for i = 1:length(y)
dxy(i) = linspace(x, y(i), n);
end
but got the error "Subscripted assignment dimension mismatch."

サインインしてコメントする。


Stephen23
Stephen23 2017 年 8 月 29 日
編集済み: Stephen23 2017 年 8 月 29 日
An efficient general solution for any size y using linspace and bsxfun:
>> 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 件のコメント
Andrew Poissant
Andrew Poissant 2017 年 8 月 29 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by