How do I iterate if I have points from [-20,5,0] to [20,5,0]?
조회 수: 2 (최근 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?
댓글 수: 0
채택된 답변
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
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 Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!