finding neighborhood in a specific location

前端 未结 1 1860
天命终不由人
天命终不由人 2021-01-27 03:56

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

1条回答
  •  孤独总比滥情好
    2021-01-27 04:31

    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);
    

    0 讨论(0)
提交回复
热议问题