フィルターのクリア

How to create array of linearly spaced values from starting and ending points

196 ビュー (過去 30 日間)
Hello,
I want to create an array (not vector) of linearly space points from a vector of starting and ending points. For example, I want to do the equivalent of,
go = [1;2;3]; %starting point
st = [2;3;4]; %ending points
nGo = length(go); %number of starting points
nPoints = 10; %number of points in each row
for iGo = 1:nGo
A(iGo,:) = linspace(go,st,nPoints); %create array of linearly spaced rows
end
The problem is that the number of start points in on the order of 10000 and it is part of a fitting routine. So, I need the creation of this matrix to be as efficient as possible using minimal loops. Any suggestions?

採用された回答

Kelly Kearney
Kelly Kearney 2017 年 10 月 25 日
This method is faster than looping over linspace:
x = linspace(0,1,nPoints);
A2 = go + x.*(st - go);
Here's a timing test for nGo = 10000:
nGo = 10000;
go = randi(100, nGo, 1);
st = go + randi(100, nGo, 1);
nPoints = 10; %number of points in each row
tic
for ii = 1:100
A = nan(nGo, nPoints);
for iGo = 1:nGo
A(iGo,:) = linspace(go(iGo),st(iGo),nPoints); %create array of linearly spaced rows
end
end
t(1) = toc;
tic
for ii = 1:100
x = linspace(0,1,nPoints);
A2 = go + x.*(st - go);
end
t(2) = toc;
fprintf('Method 1: %.4f msec\nMethod 2: %.4f msec\nSpeedup: %.2f x\n', t*10, t(1)/t(2));
On my computer, I get:
Method 1: 9.6675 msec
Method 2: 0.0676 msec
Speedup: 143.07 x
Differences between A and A2 are on order of 1e-16.
  2 件のコメント
Ajay Kumar Sandula
Ajay Kumar Sandula 2020 年 10 月 10 日
This method is giving error
Walter Roberson
Walter Roberson 2021 年 7 月 5 日
I am not seeing any error when I execute ?
nGo = 10000;
go = randi(100, nGo, 1);
st = go + randi(100, nGo, 1);
nPoints = 10; %number of points in each row
tic
for ii = 1:100
A = nan(nGo, nPoints);
for iGo = 1:nGo
A(iGo,:) = linspace(go(iGo),st(iGo),nPoints); %create array of linearly spaced rows
end
end
t(1) = toc;
tic
for ii = 1:100
x = linspace(0,1,nPoints);
A2 = go + x.*(st - go);
end
t(2) = toc;
fprintf('Method 1: %.4f msec\nMethod 2: %.4f msec\nSpeedup: %.2f x\n', t*10, t(1)/t(2));
Method 1: 9.8034 msec Method 2: 0.1712 msec Speedup: 57.25 x

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

その他の回答 (2 件)

Nathan Hardenberg
Nathan Hardenberg 2021 年 7 月 5 日
編集済み: Nathan Hardenberg 2021 年 7 月 5 日
I want to add the same function/functionality for Simulink. Since the given answer does not seem to work with a Simulink Matlab-function I wrote my own code. [The transposing (transpose() or ') in the beginning is nessecary since simulink seems to automatically assume a row vector.]
Since the output vector is of variable size it's important to set the output (y) as variable size an give a maximum size in the Size-field: (See also: https://de.mathworks.com/matlabcentral/answers/654813-how-do-i-resolve-an-error-about-variable-sized-data-in-my-matlab-function-block?s_tid=srchtitle)
Variable names corresponding to post:
go -> start | st -> goal | nPoints -> steps
See Comments for a slightly better Version!!!! Click Here
function y = vec_linspace(start, goal, steps)
start = start';
goal = goal';
x = linspace(0,1,steps);
% difference = (goal - start);
%
% multip = difference'*x;
%
% onesvec = ones(1, steps);
% startvec = start' * onesvec;
%
% y = startvec + multip;
y = start' * ones(1, steps) + (goal - start)'*x;
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 7 月 5 日
Could you confirm that you really want to transpose the original start, and then that you want to transpose again when you use it in calculating y ? Wouldn't you get the same result if you did
function y = vec_linspace(start, goal, steps)
x = linspace(0,1,steps);
% difference = (goal - start);
%
% multip = difference'*x;
%
% onesvec = ones(1, steps);
% startvec = start' * onesvec;
%
% y = startvec + multip;
y = start * ones(1, steps) + (goal - start)*x;
Nathan Hardenberg
Nathan Hardenberg 2021 年 7 月 5 日
Hmm, seems you are right! Your version works as well and is also better (less calculations). While debugging I had a lot of issues with matrix multiplications not working, so i probably did unnecessary transposes.

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


Ashaq Shah
Ashaq Shah 2022 年 1 月 8 日
編集済み: Ashaq Shah 2022 年 1 月 8 日
vector = [startpoint:step:endpoint]

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by