I have a 2D matrix and I want to find the neighborhood of (i,j) in this matrix with M and N sizes in x and y directions, respectively. I know that it is easy to do, but my probl
If I understand correctly, you would like to extract a submatrix from a matrix, with the submatrix centered going from row i-M
to i+M
and column j-N
to j+N
.
If this is the case and you would like to avoid selecting invalid indices, you can chop the selection using min/max functions, eg:
matrix = randi(10,20,15);
siz = size(matrix);
i=2;
j=5;
M=10;
N=3;
selectrows = max(1,i-M):min(siz(1),i+M);
selectcols = max(1,j-N):min(siz(2),j+N);
result = matrix(selectrows, selectcols);