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

2 vues (au cours des 30 derniers jours)
Natalie Arroyo-Rivera
Natalie Arroyo-Rivera le 7 Sep 2020
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?

Réponse acceptée

John D'Errico
John D'Errico le 7 Sep 2020
Modifié(e) : John D'Errico le 7 Sep 2020
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.

Plus de réponses (1)

KSSV
KSSV le 7 Sep 2020
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 ;

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by