簡體   English   中英

如何確定一維數組中的相鄰元素?

[英]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 中執行此操作的邏輯。

確定執行此操作的邏輯

  • 具有 6 個項目的一維數組具有索引 - [0,1,2,3,4,5]
  • 建議的形狀是 2 行和 3 列 - 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
  • 這些 2d 索引需要用(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 后,您需要檢查並丟棄一些沒有意義的。

    • 第 4 項位於右上角,沒有一些鄰居,例如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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM