MATLAB: How to create a matrix in which the elements are the distance to a point in the matrix using the Image Processing Toolbox 6.3 (R2009a)

Image Processing Toolbox

I would like to create a matrix in which I can mark an element with a zero, and all the elements around it contain the distance to the element 0, like in the following example:
2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

Best Answer

You can create a matrix such as the one in the example using the function BWDIST (Distance transform of binary image) as follows:
% Create empty matrix.
a = zeros(5);
% Mark the center.
a(3,3) = 1;
% Transform the matrix.
b = bwdist(a,'chessboard')