Plotting multiple objects on same axis

23 ビュー (過去 30 日間)
Jide Williams
Jide Williams 2018 年 10 月 26 日
コメント済み: Jide Williams 2018 年 10 月 26 日
I am new to MATLAB but I am trying to plot multiple circles on the same axis. I am using rand function to generate the centers because I want some of them to overlap, but it ends up plotting just one circle. Below is my code;
r = 0.005; x = rand(100,1); y = rand(100,1); th = 0:pi/100:2*pi; for i = 1:length(x) & 1:length(y) a = r*cos(th) + x(i); b = r*sin(th) + y(i); axis tight; hold on plot(a,b) end

採用された回答

Kevin Chng
Kevin Chng 2018 年 10 月 26 日
編集済み: Kevin Chng 2018 年 10 月 26 日
r = 0.005;
x = rand(100,1);
y = rand(100,1);
th = 0:pi/100:2*pi;
for i = 1:1:length(x)
a = r*cos(th) + x(i);
b = r*sin(th) + y(i);
figure(1)
axis tight;
hold on
plot(a,b)
pause(0.1)
end
Slight change your code, it is very interesting, if you add pause(), then you see the animation. However, you delete the pause() if you don't want it.

その他の回答 (3 件)

madhan ravi
madhan ravi 2018 年 10 月 26 日
編集済み: madhan ravi 2018 年 10 月 26 日
r = 0.005;
x = rand(100,1);
y = rand(100,1);
th = 0:pi/100:2*pi;
a=zeros(1,numel(x)) % preallocation for speed and efficiency
b= zeros(1,numel(y))
for i = 1:numel(x)
a(i) = r*cos(th(i)) + x(i);
b(i) = r*sin(th(i)) + y(i);
end
axis tight;
hold on
plot(a,b,'ob')
  7 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 26 日
Wow thanks Jonas completely agree with you , one thing experience is gained day by day :)

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


jonas
jonas 2018 年 10 月 26 日
If you have the image processing toolbox available, then there is a one-line solution to your problem.
viscircles([x,y],ones(size(x)).*r)
  6 件のコメント
Jide Williams
Jide Williams 2018 年 10 月 26 日
Thanks again

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


Jide Williams
Jide Williams 2018 年 10 月 26 日
Ok, Thanks

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by