# How to create a circle within a matrix

5 ビュー (過去 30 日間)
Edward Villanueva 2019 年 12 月 7 日
Commented: Edward Villanueva 2019 年 12 月 7 日
Need to figure out how to create a circle of a specificed size and position, within a premade matrix of zero's. Currently I have the code for the Matrix set and I am able to control the boundaries of the maxtrix. but I would like to be able to place a circle matrix of desired radius and location within the maxtrix of zero's Ive specified
cx1 = .01; %x position of circle
cy1 = .05; %y position of circle
cr1 = .02; %radius of circle
th = 0:pi/100:2*pi;
xunit = cr1 * cos(th) + cx1
yunit = cr1 * sin(th) + cy1
plot (xunit,yunit)
x=-.10:.02:.10;
[X,Y]=meshgrid(x); % xy space
v1=zeros(size(X)); % previous v
v1(xunit,yunit)=10 % top boundary CONTROLS COLOR BAR
v1(end,:)=10 % bottom boundary
v1(:,1)=10 % left boundary
v1(:,end)=10 % right boundary

#### 0 件のコメント

サインイン to comment.

### 採用された回答

Walter Roberson 2019 年 12 月 7 日

x=-.10:.002:.10; %note change from 0.02 to 0.002
[X,Y]=meshgrid(x); % xy space
v1=zeros(size(X)); % previous v
xidx = interp1(x, 1:length(x), xunit, 'nearest');
yidx = interp1(x, 1:length(x), yunit, 'nearest');
idx = sub2ind(size(X), xidx, yidx);
v1(idx) = 10;
v1(end,:)=10; % bottom boundary
v1(:,1)=10; % left boundary
v1(:,end)=10; % right boundary
You were painting a circle with radius 0.02 into a location with pixels spaced 0.02 apart. The result was essentially unrecognizable as a circle. You can switch the resolution back in the definition of x if you want but you will not see anything useful.
By the way, if you have the Computer Vision toolbox, use insertShape instead of all of this.

#### 1 件のコメント

Edward Villanueva 2019 年 12 月 7 日
Thank you!

サインイン to comment.