避免sub2ind和ind2sub [英] Avoiding sub2ind and ind2sub

查看:102
本文介绍了避免sub2ind和ind2sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在3D的某个点附近访问多个索引. 例如,对于点(x1y1z1),我需要获取其3x3x3邻域的所有索引,以使(x1y1z1)居中.对于3号邻域,我用

I need to access several indices around a certain point in 3D. For example, for point (x1,y1,z1) I need to get all the indices of its 3x3x3 neighborhood such that (x1,y1,z1) is centered. For neighborhood of size 3, I do it with

 [x,y,z] = meshgrid(-1:1,-1:1,-1:1);
 x_neighbors = bsxfun(@plus,x,x1);
 y_neighbors = bsxfun(@plus,y,y1);
 z_neighbors = bsxfun(@plus,z,z1);

在这里,我通过将(x1y1z1)到3x3x3中任意点的距离相加,将x1y1z1居中到(0,0,0)盒子.

Here, I center x1,y1,z1 to (0,0,0) by adding the distances from (x1,y1,z1) to any point in the 3x3x3 box.

这给了我(x1y1z1)3x3x3邻域的坐标.然后,我需要将它们转换为线性索引,以便可以访问它们:

that gives me the coordinates of (x1,y1,z1) 3x3x3 neighborhood. I then need to turn them into linear indices so I can access them:

 lin_ind = sub2ind(size(volume),y_neighbors,x_neighbors,z_neighbors);

那是我做的昂贵的事.

我的问题是,如何避免使用sub2ind.如果inx是(x1y1z1)的线性索引,

My question is, how to avoid sub2ind. If inx is the linear index of (x1,y1,z1),

inx = sub2ind(size(volume),y1,x1,z1);

如何通过加或减或inx的任何其他简单操作找到线性索引的3x3x3邻域?

how can I find the 3x3x3 neighborhood of the linear index by adding or subtracting or any other simple operation of inx?

推荐答案

只要知道3D数组的尺寸,就可以计算3x3x3邻域所有元素的线性偏移.为了说明这一点,请考虑4x5矩阵的2D示例.线性索引如下所示:

As long as you know the dimensions of your 3D array, you can compute the linear offsets of all the elements of the 3x3x3 neighborhood. To illustrate this, consider a 2D example of a 4x5 matrix. The linear indices look like this:

1 5  9 13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20

10的3x3邻域是[5 6 7 9 10 11 13 14 15]. 15的3x3邻域是[10 11 12 14 15 16 18 19 20].如果减去中心元素的索引,则在两种情况下均得到[-5 -4 -3 -1 0 1 3 4 5].更一般而言,对于M x N矩阵,我们将具有[-M-1 -M -M+1 -1 0 1 M-1 M M+1][(-M+[-1 0 1]) -1 0 1 (M+[-1 0 1])].

The 3x3 neighborhood of 10 is [5 6 7 9 10 11 13 14 15]. The 3x3 neighborhood of 15 is [10 11 12 14 15 16 18 19 20]. If we subtract off the index of the central element, in both cases we get [-5 -4 -3 -1 0 1 3 4 5]. More generally, for MxN matrix we will have [-M-1 -M -M+1 -1 0 1 M-1 M M+1], or [(-M+[-1 0 1]) -1 0 1 (M+[-1 0 1])].

概括为三个维度,如果数组为M x N x P,则从中心元素开始的线性索引偏移将为[(-M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1]) [-M-1 -M -M+1 -1 0 1 M-1 M M+1] (M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1])].您可以根据需要将其重塑为3x3x3.

Generalizing to three dimensions, if the array is MxNxP, the linear index offsets from the central element will be [(-M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1]) [-M-1 -M -M+1 -1 0 1 M-1 M M+1] (M*N+[-M-1 -M -M+1 -1 0 1 M-1 M M+1])]. You can reshape this to 3x3x3 if you wish.

请注意,这种索引不能很好地处理边缘.如果要在数组边缘找到元素的邻居,则可能应该先在所有边上填充数组(从而更改MNP).

Note that this sort of indexing doesn't deal well with edges; if you want to find the neighbors of an element on the edge of the array you should probably pad the array on all sides first (thereby changing M, N, and P).

这篇关于避免sub2ind和ind2sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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