绘制多个圆

Plotting multiple circles

我想绘制多个以先前已确定的质心为中心的圆。

所以我有这段代码,它使用质心作为圆的中心,但我得到了错误:"Error using + Matrix dimensions must agree."

r = 4;
cen_x = centroid(:,1);
cen_y = centroid(:,2);
th = 0:pi/50:2*pi;
xunit = r * cos(th) + cen_x;
yunit = r * sin(th) + cen_y;
hold on
h = plot(xunit, yunit);

有什么帮助吗?

您正在尝试将 cos(th)cen_x 相加,但它们的尺寸不匹配。您将需要使用 bsxfun 以便正确广播维度。

xunit = bsxfun(@plus, cen_x, r * cos(th)).';   
yunit = bsxfun(@plus, cen_y, r * sin(th)).';

plot(xunit, yunit)

您也可以使用 rectangle 为您画圈。你也可以指定一个FaceColor来填充它们。

positions = [centroid - (r/2), r + zeros(size(centroid))];

for k = 1:size(positions, 1)
    rectangle('Position', positions(k,:), 'Curvature', [1 1], 'FaceColor', 'r');
end