Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

How to MATLAB scatter3, plot3 speed discrepencies

Date: 2022-08-05 10:59:47

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. 

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');
    drawnow;
end

which forces Matlab to update the figure at every iteration (and which is A LOT slower), you'll see that the axes limits don't change at all (since the default axes limits are 0 and 1). However, even if the axes limits started out differently, it wouldn't take many iterations for them to converge with these data. Compare with omitting the hold on, which makes plotting take longer, because axes are recalculated at every step.

Why have these different functions? scatter3 allows you to plot points with different marker sizes, and colors under a single handle, while you'd need a loop and get a handle for each point using plot3, which is not only costly in terms of speed, but also in terms of memory. However, if you need to interact with different points (or groups of points) individually - maybe you want to add a separate legend entry for each, maybe you want to be able to turn them on and off separately etc - using plot3 in a loop may be the best (though slow) solution. 

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com