How do I iterate if I have points from [-20,5,0] to [20,5,0]?

3 ビュー (過去 30 日間)
Natalie Arroyo-Rivera
Natalie Arroyo-Rivera 2020 年 9 月 7 日
These points are coordinate points xyz and I need to create a function or loop to find velocities in a line starting from [-20,5,0] to [20,5,0]?
s_vector=[-20,5,0]; % line starts
e_vector=[20,5,0]; % line ends
for i=linspace(s_vector(1),e_vector(1))
disp(i)
end
Is there any other way to do this, to account for all xyz components?

採用された回答

John D'Errico
John D'Errico 2020 年 9 月 7 日
編集済み: John D'Errico 2020 年 9 月 7 日
First, learn to use MATLAB as a matrix language. That often means not thinking in terms of loops.
s_vector=[-20,5,0]; % line starts
e_vector=[20,5,0]; % line ends
npts = 41; % this will give nice increments, since only x is varying
t = linspace(0,1,npts).';
xyz = s_vector + t*(e_vector - s_vector);
Rather than dump the entire array of points to the screen, I'll just give you the first 5:
xyz(1:5,:)
ans =
-20 5 0
-19 5 0
-18 5 0
-17 5 0
-16 5 0
The virtue of this scheme is it works for ANY pair of start and end points, in any number of dimensions.

その他の回答 (1 件)

KSSV
KSSV 2020 年 9 月 7 日
clc; clear all ;
x1 = -20 ; y1 = 5 ;
x2 = 20; y2 = 5 ;
t = linspace(0,1) ;
x = x1+(x2-x1)*t ;
y = y1+(y2-y1)*t ;

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by