Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:09:44+00:00 2026-05-24T22:09:44+00:00

I need to plot a list of 3d lines in matlab. What is the

  • 0

I need to plot a list of 3d lines in matlab. What is the quickest way to do that?
I am currently doing something like

%edges is a MX2 matrix, holding the list of edges
%points are the vertices' coordinates
hold on; %so all the lines will be saved
for i=1:size(edges,1)
    a=edges(i,1); %get first point's index
    b=edges(i,2); %get second point's index
    p=[points(:,a) points(:,b)]; %construct a 3X2 matrix out of the 2 points
    plot3(p(1,:),p(2,:),p(3,:)); %plot a line
end

But this is not only slow during the actual loop, but also at the end, the resulting plot is very slow and irresponsive when I try to, for instance, rotate it using the drag & rotate tool.

I know the same plot using opengl etc would run much faster…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:09:46+00:00Added an answer on May 24, 2026 at 10:09 pm

    I think you can do something like this (caution – brain compiled code…)

    figure;
    patch('faces', edges, 'vertices', points, 'edgecolor', 'b');
    axis equal;
    

    Where edges should be an Nx2 matrix of indices and points should be an Mx3 matrix of coordinates (the transpose of your points array).

    From my experience, calling patch directly can be significantly faster than repeated calls to plot.

    To give some idea, the times to generate 1000 randomly generated line segments, using my (admittedly old!) MATLAB 7.1 are as follows:

    1. Calling patch: 0.03 seconds.
    2. Calling plot: 0.5 seconds.

    EDIT: One way to get the edge colour behaving as you want (specifying a single colour per edge) is to introduce duplicate vertices as follows:

    This works-around the issue that the edge colour can only be specified indirectly via vertex colour data. If we were to rely only on the vertex colours then all edges sharing a common vertex might end up with the colour assigned to that vertex – check out the ‘flat ‘edgecolour description here.

    %% a "star" shape, so that we can really see what's going on 
    %% with the edge colours!!
    pp = [0,0,0; 1,-1,0; 1,1,0; -1,1,0; -1,-1,0];
    ee = [1,2; 1,3; 1,4; 1,5];
    
    %% important - only 1 colour known per edge, not per vertex!!
    cc = (1:size(ee,1))'; 
    
    %% setup a new set of vertices/edges/colours with duplicate vertices
    %% so that each edge gets it's correct colour
    nnum = 0;
    pnew = zeros(2 * size(ee, 1), 3); %% new vertices
    enew = zeros(1 * size(ee, 1), 2); %% new edge indices
    cnew = zeros(2 * size(ee, 1), 1); %% new edge colours - via vertices
    for j = 1 : size(ee, 1)
        n1 = ee(j, 1); %% old edge indices
        n2 = ee(j, 2);
        enew(j, 1) = nnum + 1; %% new edge indicies into pnew
        enew(j, 2) = nnum + 2;
        pnew(nnum + 1, :) = pp(n1, :); %% create duplicate vertices
        pnew(nnum + 2, :) = pp(n2, :);
        cnew(nnum + 1) = cc(j); %% map single edge colour onto both vertices
        cnew(nnum + 2) = cc(j);
        nnum = nnum + 2;
    end
    
    %% Draw the set efficiently via patch
    tic
    figure;
    hold on;
    patch('faces', enew, 'vertices', pnew, 'facevertexcdata', cnew, ...
        'edgecolor', 'flat', 'facecolor', 'none');
    plot(pnew(:,1), pnew(:,2), 'b.');
    axis equal;
    toc
    

    It would be nicer if MATLAB allowed you to directly specify the edge colour data – but it doesn’t seem to support that…

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need plot a curve for my data , the source is like :
Need a way to allow sorting except for last item with in a list.
I need to plot some data in various forms. Currently I'm using Matplotlib and
I have a list of RGB triplets, and I'd like to plot them in
I need some help in Mathematica. I'm trying to plot functions that are stored
I need to plot a dome (or half sphere) that have different radii. I
I need to be able to plot lines on three axes with negative as
I need to plot multiple sphere and I was using the example code from
Hi I need to plot a boxplot in R. I have two matrices a
I need to read some data from an input file and plot a graph

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.