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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by