plotting N number of lines without for-loops

8 ビュー (過去 30 日間)
Oscar Frick
Oscar Frick 2018 年 6 月 22 日
コメント済み: Dieter Karst 2020 年 5 月 13 日
I have two matrixes of size (N,3), "S" corresponding to line start points "E" to line end points. I want to plot these lines, and know I can do it using
hold on
N = size(S,1);
for i = 1:N
plot3( [S(i,1),E(i,1)] , [S(i,2),E(i,2)] , [S(i,3),E(i,3)] )
end
Is there a way to achieve this without using for-loops? I have heard that inputing matrixes should have Matlab interpret the columns as several lines, but I have not gotten it to work properly.

採用された回答

KSSV
KSSV 2018 年 6 月 22 日
編集済み: KSSV 2018 年 6 月 22 日
S = [0 0 0] ;
N = 10 ;
E = rand(N,3) ;
figure
hold on
S = repmat(S,N,1) ;
x = [S(:,1) E(:,1)] ;
y = [S(:,2) E(:,2)] ;
z = [S(:,3) E(:,3)] ;
plot3(x',y',z')
  1 件のコメント
Oscar Frick
Oscar Frick 2018 年 6 月 22 日
That is exactly what I tried doing, but I couldn't get it to work properly. I must have messed up something in the syntax.

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 6 月 22 日
編集済み: Stephen23 2018 年 6 月 22 日
Simpler:
>> N = 5;
>> S = randi(9,N,3);
>> E = randi(9,N,3);
>> A = permute(cat(3,S,E),[3,1,2]);
>> plot3(A(:,:,1),A(:,:,2),A(:,:,3))
Giving:

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by