Plotting multiple objects on same axis

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 日

0 投票

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 日

1 投票

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 件のコメント

Jide Williams
Jide Williams 2018 年 10 月 26 日
Absolutely! Thank you, You know i thought of this, but just wasn't aware of the numel function. Thanks again
madhan ravi
madhan ravi 2018 年 10 月 26 日
編集済み: madhan ravi 2018 年 10 月 26 日
Anytime:)
madhan ravi
madhan ravi 2018 年 10 月 26 日
its always faster to use numel() for vector than length()
jonas
jonas 2018 年 10 月 26 日
編集済み: jonas 2018 年 10 月 26 日
Just a note, as far as I can see you are not plotting the actual radius here. The radius of the circles is defined by the markersize, and not by the variable r. Therefore, if you zoom in the radius changes. In the other answer, the radius is constant, and defined by r. Perhaps I misunderstood the question.
madhan ravi
madhan ravi 2018 年 10 月 26 日
the actual question was to plot a point of circles centre point and not the radius of the circle as far as I understood
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 日

1 投票

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 Jonas and noted....hmm so the code may look like viscircles([x(i),y(i)], ones(size(x(i))).*r? or can you help define your variables? Thanks
jonas
jonas 2018 年 10 月 26 日
Well, the full code would read
r = 0.005;
x = rand(100,1);
y = rand(100,1);
viscircles([x,y],ones(size(x)).*r)
Jide Williams
Jide Williams 2018 年 10 月 26 日
And what does r in my code stands for then? A multiplyer?
jonas
jonas 2018 年 10 月 26 日
r is a scalar, but the input to the function must be a vector of the same size as x and y. That's why you simply multiply it by a number of ones, like:
1 0.005
1 *. 0.005 = 0.005
1 0.005
... ...
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 日

0 投票

Ok, Thanks

カテゴリ

ヘルプ センター および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by