MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# create a white circle or sphere inside a black box

Asked by lara on 21 Jul 2011

i have created white boxes inside a big black box as follows:

A=zeros(70,70); % black box

A(15:23,50:55)=1;% white box

A(50:60,50:55)=1;% white box

A(20:23,10:13)=1 ;% white box

imshow(A,[])

how can i add to the same matrix A a white circle or a white sphere?thanks in advance.

## Products

No products are associated with this question.

## 4 Answers

Answer by Andrei Bobrov on 21 Jul 2011
Accepted answer
```A=zeros(70,70);
r = 10; %radius
m = {40,40}; %midpoint
A(m{:})=1;
B = imdilate(A,strel('disk', r,0) );
imshow(B)
```

ADD wutout Image Processing Toolbox

```A=zeros(70,70);
r = 10; %radius
P = [40,40]; %midpoint
[m n ] = size(A);
X = bsxfun(@plus,(1:m)',zeros(1,n));
Y = bsxfun(@plus,(1:n),zeros(m,1));
B = sqrt(sum(bsxfun(@minus,cat(3,X,Y),reshape(P,1,1,[])).^2,3))<=r;
imagesc(B)
```

## 4 Comments

Andrei Bobrov on 21 Jul 2011

A=zeros(70,70);
r = 10; %radius
m = {40,40}; %midpoint
A(m{:})=1;
B = bwdist(A) <= r;
imshow(B)

Friedrich on 21 Jul 2011

Nice. +1

Andrei Bobrov on 21 Jul 2011

Thanks, Friedrich!
Adding my variant without Image Processing Toolbox

Answer by Friedrich on 21 Jul 2011

Hi,

can this help?

```function out = my_circ( A, midpoint, radius )
```
```out = A;
[ m n] = size(A);
```
```for i=1:m
for j=1:n
if norm( [i,j] - midpoint ) <= radius
out(i,j) = 1;
end
end
end
```
```end
```

And call it through:

```>> A = zeros(100);
>> B = my_circ(A,[40,40],10);
>> imshow(B,[]);
```

## 0 Comments

Answer by Ujitha on 3 Mar 2013

Hi this was really helpful. How do you do this to create two different sizes of circles.

Thanks inadvance

## 1 Comment

Answer by Ujitha on 4 Mar 2013

Thanks a lot. It was really helpful..!

Contact us