在特定位置寻找邻居 [英] finding neighborhood in a specific location

查看:131
本文介绍了在特定位置寻找邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D矩阵,我想在这个矩阵中找到(i,j)的邻域,分别在x和y方向上有M和N个大小。我知道这很容易做到,但我的问题是,当(i,j)接近角落时,M和N很大!在这种情况下,我不想超过矩阵。在MATLAB中是否存在针对此问题的任何函数或简单解决方案?

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 problem is that when (i,j) is close to corners and M and N are large! In this case I do not want to exceed the matrix. Is there any function or simple solution for this problem in MATLAB?

推荐答案

如果我理解正确,您想提取子矩阵从矩阵开始,子矩阵居中从行 iM i + M 和列 jN j + N

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

这篇关于在特定位置寻找邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆