How do I iterate if I have points from [-20,5,0] to [20,5,0]?
2 views (last 30 days)
Show older comments
Natalie Arroyo-Rivera
on 7 Sep 2020
Commented: Natalie Arroyo-Rivera
on 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?
0 Comments
Accepted Answer
John D'Errico
on 7 Sep 2020
Edited: John D'Errico
on 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.
More Answers (1)
KSSV
on 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 ;
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!