MATLAB: Sub2ind gives an error: Error using sub2ind (line 43) Out of range subscript. How to solve this

circlepol2cartsub2ind

I have got the following code:
K=zeros(100,100);
theta=0:.1:7;
radius=10;
[X,Y] = pol2cart(theta,radius);
A=[round(X)+10];
B=[round(Y)+10];
linearIndex = sub2ind(size(K),A,B);
K(linearIndex) = 30;
When I run the script I get the following error: Error using sub2ind (line 43) Out of range subscript.
How do I solve this problem? Thanx for help 🙂
My end goal is to write code, that creates a circle in a matrix.

Best Answer

Part of your circle has indices of zero value which means the circle extends beyond the limits of K. Move it a little further than 10 in each direction.