I have two 3d points, A(1,0,0) and B(0,1,0). And I connect these two points as a line AB. How to divide this line into 10 equally segments and get their coordinates?

6 ビュー (過去 30 日間)
Hello,
I have two 3d points, A(1,0,0) and B(0,1,0). And I connect these two points as a line AB. How to divide this line into 10 equally segments and get their coordinates?
Many thanks!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 28 日
編集済み: Ameer Hamza 2020 年 11 月 28 日
This is one way
A = [1,0,0];
B = [0,1,0];
n = 10;
X = [A; B];
t = linspace(0, 1, n+1);
points = interp1([0 1], X, t)
Result
>> points
points =
1.0000 0 0
0.9000 0.1000 0
0.8000 0.2000 0
0.7000 0.3000 0
0.6000 0.4000 0
0.5000 0.5000 0
0.4000 0.6000 0
0.3000 0.7000 0
0.2000 0.8000 0
0.1000 0.9000 0
0 1.0000 0

その他の回答 (2 件)

M.Many
M.Many 2020 年 11 月 28 日
Hi, you can use vectors to solve this problem
AB = [-1 1 0]' %vector AB
discretization = linspace(0,1,10) %10 values
vectors = AB*discretization %matrix multiplication
This gives the matrix whose columns are the vectors AP, to get the coords of the points P just add the coords of A to the columns of 'vectors'
  3 件のコメント
M.Many
M.Many 2020 年 11 月 28 日
Yes, you can do this if you add these lines at the end of the code
AB = [-1 1 0]' %vector AB
discretization = linspace(0,1,11) %11 values for 10 segments
vectors = AB*discretization %matrix multiplication
A = [1,0,0]'
P = A+vectors
The result is
P =
1.0000 0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.1000 0
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
0 0 0 0 0 0 0 0 0 0 0
Where each culumn is the coordinates of corresponding point P
Alan
Alan 2020 年 11 月 28 日
Yes, I got it. This method works well too. Thanks!

サインインしてコメントする。


Jan
Jan 2020 年 11 月 28 日
編集済み: Jan 2020 年 11 月 30 日
A = [1, 0, 0];
B = [0, 1, 0];
ab = B - A;
v = linspace(0, 1, 10).';
C = A + v .* ab % >= R2016b, auto-expanding
% For older versions:
C = A + bsxfun(@times, v, ab)
  2 件のコメント
Alan
Alan 2020 年 11 月 28 日
When I input these code, the comment window shows information: Matrix dimentsions must agree.
Jan
Jan 2020 年 11 月 30 日
Which Matlab version are you using? The above code requires >= R2016b.

サインインしてコメントする。

カテゴリ

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