[英]How to zip each element from a 1D array with the elements from each row of a 2D array?
[英]How to determine neighboring elements from a 1D array?
我正在研究有限元分析代码,我目前有一个列出元素密度值的一维数组,如下所示:
x = np.ones(12) 其中索引是元素编号 0, 1, 2, ..., 10, 11
绘制时的元素是这样的:
0 - 3 - 6 - 9
1 - 4 - 7 - 10
2 - 5 - 8 - 11
我设置了 x 和 y 方向上的元素数量(对于这种情况,x 方向为 4,y 方向为 3)但是很难确定环绕元素。 我需要找到一种方法来确定围绕给定元素的 3、5 或 8 个元素。 For example, if I select element 0 the surrounding elements are 1, 3, 4 or if I select element 6 the surrounding elements are 3, 4, 7, 9, 10 or if if I select element 7 the surround elements are 3, 4 , 5, 6, 8, 9, 10, 11...
这里的最终目标是放入一个半径,并根据它确定选定元素周围的元素编号。 对此的任何建议或帮助将不胜感激。 由于某种原因,我无法确定在 python 中执行此操作的逻辑。
确定执行此操作的逻辑
[0,1,2,3,4,5]
M,N = 2,3
。i
),它的行和列是c,r = divmod(i,M)
cplus,cminus = c + 1, c - 1 rplus, rminus = r + 1, r - 1 cplus,r cminus,r c,rplus c,rminus cplus,rplus cplus,rminus cminus,rplus cminus,rminus
(col * M) + row
转换为 1d 索引例如
[0,1,2,3,4,5]
M,N = 2,3
'''
0 2 4
1 3 5
'''
项目 4 的 2d 索引是c,r = divmod(4,M)
--> (2,0)
(col,row)
其邻居的二维索引之一是c,rplus
--> (2,1)
那个邻居的一维索引是(2 * M) + 1
--> 5
将邻居的 2d 索引转换为 1d 后,您需要检查并丢弃一些没有意义的。
c,rminus
,这将是(2,-1)
,这没有意义。 或者cplus,r
... (3,0)
这也没有意义。警告 - 我没有尝试彻底测试这一点。
这是一个返回可调用对象的 function。
import operator
def get_neighbors(index, shape=(M,N)):
'''Returns a callable.
(M,N) --> (number_of_rows, number_of_columns)
'''
M, N = shape
# 2d index
c, r = divmod(index, M)
print(f'2d index: {(c,r)}')
# neighbors
cplus, cminus = c + 1, c - 1
rplus, rminus = r + 1, r - 1
# dot product of (c,cplus,cminus) and (r,rplus,rminus)?
neighbors = [
(cminus, rminus),
(cminus, r),
(cminus, rplus),
(c, rplus),
(cplus, rplus),
(cplus, r),
(cplus, rminus),
(c, rminus),
]
# print(neighbors)
# validate/filter
neighbors = [
(col, row) for col, row in neighbors if (0 <= col < N) and (0 <= row < M)
]
# print(neighbors)
# 1d indices
one_d = [(col * M) + row for col,row in neighbors]
# print(one_d)
return operator.itemgetter(*one_d)
试试看。
>>> a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
>>> M,N = 4,5 # nrows, ncols
'''
[['a' 'e' 'i' 'm' 'q']
['b' 'f' 'j' 'n' 'r']
['c' 'g' 'k' 'o' 's']
['d' 'h' 'l' 'p' 't']]
'''
>>> # i's neighbors
>>> q = get_neighbors(a.index('i'),(M,N))
2d index: (2, 0)
>>> q(a)
('e', 'f', 'j', 'n', 'm')
>>>
>>> # k's neighbors
>>> q = get_neighbors(a.index('k'),(M,N))
2d index: (2, 2)
>>> q(a)
('f', 'g', 'h', 'l', 'p', 'o', 'n', 'j')
>>>
>>> # q's neighbors
>>> q = get_neighbors(a.index('q'),(M,N))
2d index: (4, 0)
>>> q(a)
('m', 'n', 'r')
>>>
i
是不同形状的邻居
>>> M,N = 5,4
>>> q = get_neighbors(a.index('i'),(M,N))
2d index: (1, 3)
>>> q(a)
('c', 'd', 'e', 'j', 'o', 'n', 'm', 'h')
>>> M,N = 10,2
>>> q = get_neighbors(a.index('i'),(M,N))
2d index: (0, 8)
>>> q(a)
('j', 't', 's', 'r', 'h')
>>> M,N = 2,10
>>> q = get_neighbors(a.index('i'),(M,N))
2d index: (4, 0)
>>> q(a)
('g', 'h', 'j', 'l', 'k')
>>>
Numpy 文档中有一个很好的讨论,关于将 1d 事物制作/处理为 Nd 事物 - ndarray 的内部 memory 布局
您描述 1d --> 2d 转换的方式使用了列主要方案。 我习惯于以行为主- 我写了 function 来接受/期望一个 (nrows,ncols) shape
参数,但在 function 内部我有点切换到列主处理。 我不得不小心,所以也许这是一个糟糕的设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.