7 views (last 30 days)

Dheeraj Singh
on 19 Aug 2019

I understand that you want to fill the matrix such that the boundaries are filled circularly.

And for starting 3, it should be before the middle row or column and vice-versa for the closing.

So, for say matrix:

[ 0 0 0 3 3 0 0 0

0 0 3 0 0 0 3 0

0 3 0 0 0 0 3 0

3 0 0 0 0 0 0 3

3 0 0 0 0 0 0 0

3 0 0 0 0 0 0 0

0 3 0 0 0 0 3 0

0 0 0 0 0 0 3 0

0 0 0 0 0 0 0 0

0 0 3 3 0 0 0 0 ]

You would like the output to be something like this:

[ 0 0 0 3 3 0 0 0

0 0 3 0 0 3 3 0

0 3 0 0 0 0 3 0

3 0 0 0 0 0 0 3

3 0 0 0 0 0 0 3

3 0 0 0 0 0 0 3

0 3 0 0 0 0 3 0

0 3 0 0 0 0 3 0

0 3 0 0 0 0 3 0

0 0 3 3 3 3 0 0]

You can do it by dividing the whole matrix into 4 quadrants similar to the circle.

The following code below implements it for when the number of columns is greater than number of rows.

%your matrix

a=zeros(8,10);

N=a(1,:);

M=a(:,1);

m1=N/2;

e1=m1+1;

m2=M/2;

e2=m2+1;

%for first quadrant;

j=1;

for i=m2:-1:1

if a(j,i)~=3

a(j,i)=3;

end

j=j+1;

j=min([j,m1]);

end

%for 2nd quadrant

j=e1;

for i=1:m2

if a(j,i)~=3

a(j,i)=3;

end

j=j+1;

j=min([j,N]);

end

%for 3rd quadrant

j=N;

for i=e2:M

if a(j,i)~=3

a(j,i)=3;

end

j=j-1;

j=max([j,e1]);

end

%for 4th quadrant

j=m1;

for i=M:-1:e2

if a(j,i)~=3

a(j,i)=3;

end

j=j-1;

j=max([j,1]);

end

You can the modify the above code for the case where no of rows are more than columns.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734929

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734929

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734942

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734942

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734947

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_734947

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735243

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735243

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735326

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735326

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735333

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/476095-how-to-create-the-contour-closed-surface-utilizing-matlab-functions-or-loopings-using-the-given-ma#comment_735333

Sign in to comment.