MATLAB: How to create a square image in the middle of matrix 67 by 67

arrayimagematrix

i have a matrix image of 67 , 67. i want to create a square in the middle of the matrix image gow can i create that. it will be great if someone can help me out.

Best Answer

A = zeros(67,67) ;
[m,n] = size(A) ;
mx = m/2 ; my = n/2 ;
S = 5 ; % side of square
x = (mx-S/2):(mx+S/2) ;
y = (my-S/2):(my+S/2) ;
[X,Y] = meshgrid(round(x),round(y)) ;
idx = sub2ind(size(A),X(:),Y(:)) ;
A(idx) = 1 ;
imshow(A)