Yxwytz
Yxwytz

Reputation: 21

Plot several lines in Matlab without for-loop

In Matlab I have two Nx3 matrices P and Q and each line represents a point. I want to plot lines between the points that are in the same row of the matrices.

Following Code does it:

for i=1:N
  plot3( [P(i,1) Q(i,1)], ...
         [P(i,2) Q(i,2)], ...
         [P(i,3) Q(i,3)] )
end

Is there a way to do it without a for-loop?

If I give plot3 the points just as vectors, Matlab draws lines between Q(i,:) and P(i+1,:) in addition to the lines I want.

Upvotes: 2

Views: 1868

Answers (2)

Singlet
Singlet

Reputation: 309

Probably you want this:

h = quiver3(P(:,1), P(:,2), P(:,3), Q(:,1), Q(:,2) , Q(:,3),0);

set(h,'ShowArrowHead','off');

Upvotes: 0

groovingandi
groovingandi

Reputation: 2006

Try

plot3([P(:, 1) Q(:, 1)]', ...
      [P(:, 2) Q(:, 2)]', ...
      [P(:, 3) Q(:, 3)]')

If you feed a matrix to the Matlab plotting functions, each column corresponds to one line drawn.

Upvotes: 4

Related Questions