slow drawing of a set of points on a plane

2 次查看(过去 30 天)
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

采纳的回答

Aquatris
Aquatris 2024-8-23
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
timeViaForLoop = 0.6282
% vector plotting
tic
figure(2)
plot(p(1,:),p(2,:),'r.'),grid on, axis([-2 2 -2 2]),axis('square')
timeViaVectorizeCode = toc
timeViaVectorizeCode = 0.0679
  4 个评论
Aquatris
Aquatris 2024-8-23
There is matlab profiler that might come in handy, here is the link
M A
M A 2024-9-12
Dear Mr Aquatris
thankyou for the detailed answer !
manu1965@wail.ch

请先登录,再进行评论。

更多回答(1 个)

Voss
Voss 2024-8-22
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 ]
p = 2x82
1.0000 0.9950 0.9801 0.9553 0.9211 0.8776 0.8253 0.7648 0.6967 0.6216 0.5403 0.4536 0.3624 0.2675 0.1700 0.0707 -0.0292 -0.1288 -0.2272 -0.3233 -0.4161 -0.5048 -0.5885 -0.6663 -0.7374 -0.8011 -0.8569 -0.9041 -0.9422 -0.9710 0 0.0998 0.1987 0.2955 0.3894 0.4794 0.5646 0.6442 0.7174 0.7833 0.8415 0.8912 0.9320 0.9636 0.9854 0.9975 0.9996 0.9917 0.9738 0.9463 0.9093 0.8632 0.8085 0.7457 0.6755 0.5985 0.5155 0.4274 0.3350 0.2392
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot(p(1,:),p(2,:),'r.')
grid on
axis([-2 2 -2 2])
axis('square')

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by