Hello ,
I want a better speed in operating my code ,if i did enter a large number of points like 10000 it will operate in 10 mins or more and my processor is good enough ,this code is plotting points in a uniform shape like rectangular and randomly with different x and y , thanks for attention.
%%
RN=input('Enter number of points =');
U1=[];U2=[];
for i=1:RN
xa=-L/2+L*rand(1,1)+0.2*rand(1,1)-.4;
ya=-N/2+N*rand(1,1)+0.2*rand(1,1)-.4;
xra(i)=xa;yra(i)=ya; z=[xra(i) yra(i)];
sub1=z(1);sub2=z(2);U1=[U1;sub1];U2=[U2;sub2];
USERS=[U1 U2];
figure(1)
plot(xra(i),yra(i),'k.');hold on
end

 採用された回答

Daniel Shub
Daniel Shub 2012 年 4 月 25 日

0 投票

The best thing to do would be to move the plotting outside the loop:
...
end
figure(1);
plot(xra,yra,'k.');

1 件のコメント

Ahmed Hassaan
Ahmed Hassaan 2012 年 4 月 25 日
thanks

サインインしてコメントする。

その他の回答 (2 件)

Junaid
Junaid 2012 年 4 月 25 日

1 投票

Though your code requires cleaning but this code will generate exact output you are required. In your code there are many variables which are not required.
RN=input('Enter number of points =');
L=input('Enter the length x =');
N=input('Enter the length y =');
U1=[];U2=[];
xa = -L/2+L*rand(RN,1)+0.2*rand(RN,1)-.4;
ya = -N/2+N*rand(RN,1)+0.2*rand(RN,1)-.4;
xra=xa;yra=ya; z=[xra yra];
sub1=z(:,1);sub2=z(:,2);U1=[U1;sub1];U2=[U2;sub2];
USERS=[U1 U2];
figure(1)
plot(xra,yra,'k.');
Junaid
Junaid 2012 年 4 月 25 日

0 投票

I guess this can be done in one line without using for loop, and second line for plot. Could you tell us about L, N and other varibales you are using in side loop

3 件のコメント

Ahmed Hassaan
Ahmed Hassaan 2012 年 4 月 25 日
L=input('Enter the length x =');
N=input('Enter the length y =');
Ahmed Hassaan
Ahmed Hassaan 2012 年 4 月 25 日
L=input('Enter the length x =');
N=input('Enter the length y =');
Daniel Shub
Daniel Shub 2012 年 4 月 25 日
Without having timed it, my guess is the time improvement from complete vectorization will be small compared to the use of a single plot.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by