Plotting multiple objects on same axis

조회 수: 5 (최근 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
jonas
jonas 2018년 10월 26일
Yep, you are entirely correct in that it's a poorly written question. One of the things you learn on this forum is filling in blanks and interpreting ambiguities, which is actually quite useful when teaching :)
With that said, the notations r and the values of th (0 to 2pi) and the fact that OP accepted @Keving Chng's answer would imply that OP intended the radius to go as input.
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
jonas
jonas 2018년 10월 26일
Viscircles basically does the same thing as your code, but probably more effeciently. I have not actually timed and compared.
Jide Williams
Jide Williams 2018년 10월 26일
Thanks again

댓글을 달려면 로그인하십시오.


Jide Williams
Jide Williams 2018년 10월 26일
Ok, Thanks

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by