MATLAB: I want to generate a square or rectangular array. and that array should contain desired numbers in a circle or elliptical contourwith some radius and all other numbers should be zero. Can anyone help me in this

arraycontourlogical operatorsMATLAB

I have an array of values, which is either square or circular in nature, which is as a function of x and y coordinates. I want to create a circular contour that contains a desired/input values, The contour should be like circule or elliptical, in that contour the desired values should be present. In all other places the number should be zero.

Best Answer

r = 1. ;
th = linspace(0,2*pi,500)' ;
x = r*cos(th) ; y = r*sin(th) ;
m = 500 ;n = 500;
xi = linspace(min(x),max(x),m) ;
yi = linspace(min(y),max(y),n) ;
[X,Y] = meshgrid(xi,yi) ;
idx = knnsearch([X(:),Y(:)],[x, y]) ;
Z = zeros(size(X)) ;
Z(idx) = 1 ;
pcolor(Z)
shading interp