How do I fill a rectangle (or circle) in a matrix?

Imagine the following matrix.
1 1 1 1 1 1 1 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
I know the coordinates of the 10's. they are stored in A and B. How do I use the known coordinates of 10's to fill in the 1's within the square of 10's? thanx for help.
I have simplified my problem here. I have extracted the essence of the problem above. But for those interested I'll explain the following. I want to create a filled circle inside a bigger matrix. Since I have the coordinates of the outer circle, I need a way to fill in the inside. My code:
k=zeros(300,300)+200
for theta=0:.001:7;
radius=10
[X,Y] = pol2cart(theta,radius);
A=[round(X)+radius];
B=[round(Y)+radius];
ring = sub2ind(size(K),A,B);
kleur=90;
K(A,B) = kleur;
end

 Akzeptierte Antwort

Chad Greene
Chad Greene am 8 Jun. 2016
Here's one way to fill the space between the top row of 10s and the bottom row of 10s:
K= [1 1 1 1 1 1 1 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1];
for cols = 1:size(K,2);
K(find(K(:,cols)==10,1,'first'):find(K(:,cols)==10,1,'last'),cols) = 10;
end

2 Kommentare

Sybren Verrips
Sybren Verrips am 8 Jun. 2016
Bearbeitet: Sybren Verrips am 8 Jun. 2016
This is amazing, I have searched for this for weeks. Could you give a small overall explanation, what your for loop does exactly. I really want to understand and i'm relatively new to Matlab.
Also I specificly ask myself why there is a 2 after the first K. Is this a reference to the first dimension, and why? Also i ask myself why there is a 1 written whithin the codes: ==10,1,'first and ==10,1,'last'. What does this 1 stand for?
Yes, I think I can explain it.
size(K)
gives the size of K. If you don't want all the dimensions, you can specify the dimension you want. So
size(K,1)
gives the number of rows in K. Similarly,
size(K,2)
gives the number of columns. The loop says for cols = 1:size(K,2), which is "for every column in K". Here's what it does for every column in K. It says, find the first value of 10 in that column and find the last value of 10 in that column. Then fill every value from the first to the last with 10s. This might be an easier way to read it:
for cols = 1:size(K,2);
startingrow = find(K(:,cols)==10,1,'first');
endingrow = find(K(:,cols)==10,1,'last');
K(startingrow:endingrow,cols) = 10;
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Stephen23
Stephen23 am 8 Jun. 2016
Bearbeitet: Stephen23 am 8 Jun. 2016
No toolboxes and no loops are required:
>> M = [...
1 1 1 1 1 1 1 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1];
>> X = M>1;
>> M(X | cumsum(X)==1) = 10
M =
1 1 1 1 1 1 1 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
Chad Greene
Chad Greene am 8 Jun. 2016

0 Stimmen

If you have the Image Processing toolbox,
K= [1 1 1 1 1 1 1 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 1 1 1 10 1 1 1
1 1 10 10 10 10 10 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1];
K2 = 10*imfill(K==10,'holes')
Azzi Abdelmalek
Azzi Abdelmalek am 8 Jun. 2016

0 Stimmen

K=ones(30);
theta=0:.001:7;
radius=10
[X,Y] = pol2cart(theta,radius);
A=[round(X)+radius]+1;
B=[round(Y)+radius]+1;
ring = sub2ind(size(K),A',B');
kleur=90;
K(A,B) = kleur;

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by