slow drawing of a set of points on a plane
27 views (last 30 days)
Show older comments
M A
on 22 Aug 2024 at 21:16
Commented: Aquatris
on 23 Aug 2024 at 21:32
Dear Sir
please let me have a help
this is a simple set of statements that:
-define figure 1
-clear the figure
-define a matrix 2 x 82 ,where each column represent the coordinate of a point on a plane
these set of points represent a circle and a cross , centered inside
-then do a loop that draw each point at a time, as red dot
the "hold on" is needed because at each loop i wish to maintain the points already drawed
the purpose of this code is to visualize this figure first undeformed, and after, with some deformation gradient F applied to the set of points, (and) with another (more external) loop , to visualize the advance of the deformation making the gradient F varying
(F not present in the code below)
----------------------------------------
clear
figure(1),clf
p=[[cos(0:.1:1.9*pi) (-1:.2:1) 0*(-1:.2:1) ]' [sin(0:.1:1.9*pi) 0*(-1:.2:1) (-1:.2:1) ]']'
for j=1:max(size(p)) % visualize
figure(1),plot(p(1,j),p(2,j),'r.'),grid on, axis([-2 2 -2 2]),axis('square')
hold on
end
----------------------------------------
now , to draw this simple figure it take roughly 1 second , so i ask were i'm wrong , and how can I speed up the execution of this code
the specification of the pc is as follow
Device name s.
Processor Intel(R) Pentium(R) CPU G860 @ 3.00GHz 3.00 GHz
Installed RAM 4.00 GB
Device ID ....8C39
Product ID ...9
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Edition Windows 10 Home
Version 22H2
Installed on 11/7/2020
OS build 19045.4355
Experience Windows Feature Experience Pack 1000.19056.1000.0
Matlab :
ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 24.1.0.2689473 (R2024a) Update 6
MATLAB License Number: STUDENT
Operating System: Microsoft Windows 10 Home Version 10.0 (Build 19045)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 24.1 (R2024a)
Symbolic Math Toolbox Version 24.1 (R2024a)
>>
Thankyou very much
Kind Regards
manu1965
0 Comments
Accepted Answer
Aquatris
on 23 Aug 2024 at 8:19
In essence for loop are always slower. So try to vectorize your code whereever possible for efficiency, here is a good read. In your case vectorizing the code makes it ~10 times faster
p=[[cos(0:.1:1.9*pi) (-1:.2:1) 0*(-1:.2:1) ]' [sin(0:.1:1.9*pi) 0*(-1:.2:1) (-1:.2:1) ]']';
% for loop ploting
tic
figure(1)
for j=1:max(size(p)) % visualize
figure(1),plot(p(1,j),p(2,j),'r.'),grid on, axis([-2 2 -2 2]),axis('square')
hold on
end
timeViaForLoop = toc
% vector plotting
tic
figure(2)
plot(p(1,:),p(2,:),'r.'),grid on, axis([-2 2 -2 2]),axis('square')
timeViaVectorizeCode = toc
3 Comments
More Answers (1)
Voss
on 22 Aug 2024 at 22:10
p = [ cos(0:.1:1.9*pi), -1:.2:1, 0*(-1:.2:1) ; sin(0:.1:1.9*pi), 0*(-1:.2:1), -1:.2:1 ]
figure
plot(p(1,:),p(2,:),'r.')
grid on
axis([-2 2 -2 2])
axis('square')
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!