How to MATLAB scatter3, plot3 speed discrepencies

 This is about how MATLAB can take very different times to plot the same thing — and why.

I generate 10000 points in 3D space: 

 

X = rand(10000, 1);
Y = rand(10000, 1);
Z = rand(10000, 1);

I then used one of four different methods to plot this, to create a plot like so: 

I closed all figures and cleared the workspace between each run to try to ensure fairness.

Bulk plotting using scatter3: 

 

>> tic; scatter3(X, Y, Z); drawnow; toc
Elapsed time is 0.815450 seconds. 

Individual plotting using scatter3: 

 

>> tic; hold on;
for i = 1:10000
    scatter3(X(i), Y(i), Z(i), 'b');
end
hold off; drawnow; toc
Elapsed time is 51.469547 seconds.

Bulk plotting using plot3: 

 

>> tic; plot3(X, Y, Z, 'o'); drawnow; toc
Elapsed time is 0.153480 seconds.

Individual plotting using plot3: 

 

>> tic; hold on
for i = 1:10000
    plot3(X(i), Y(i), Z(i), 'o');
end
drawnow; toc
Elapsed time is 5.854662 seconds.

What is it that MATLAB does behind the scenes in the 'longer' routines to take so long? What are the advantages and disadvantages of using each method?

Edit: Thanks to advice from Ben Voigt (see answers), I have included drawnow commands in the timing — but this has made little difference to the times.  

NOTE:-


Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.    

Answers: 

The main difference between the time required to run scatter3 and plot3 comes from the fact that plot3 is compiled, while scatter3 is interpreted (as you'll see when you edit the functions). If scatter3 was compiled as well, the speed difference would be small.

The main difference between the time required to plot in a loop versus plotting in one go is that you add the handle to the plot as a child to the axes (have a look at the output of get(gca,'Children')), and you're thus growing a complicated array inside a loop, which we all know to be slow. Furthermore, you're calling several functions often instead of just once and incur thus calls from the function overhead.

Recalculation of axes limits aren't an issue here. If you run 

 

for i = 1:10000
    plot3(X(i), Y(i), Z(i), 'o'); 
SEE COMPLETE ANSWER CLICK THE LINK  
https://matlabhelpers.com/questions/how-to-matlab-scatter3-plot3-speed-discrepencies.php

Comments

Popular posts from this blog

programmingshark

Why do I get a "Too many input arguments" error when not passing any?

Constructing piecewise symbolic function in Matlab