![](/img/trans.png)
[英]How do I merge a 2D array in Python into one string with List Comprehension?
[英]How do I merge several 2D arrays into a single 2D array (list) in a box-like manner in python?
假設我有 9 個二維 arrays,格式如下:
A1 = [[ a1, b1, c1 ],
[ d1, e1, f1 ],
[ g1, h1, i1 ]]
A2 = [[ a2, b2, c2 ],
[ d2, e2, f2 ],
[ g2, h2, i2 ]]
.....
A9 = [[ a9, b9, c9 ],
[ d9, e9, f9 ],
[ g9, h9, i9 ]]
我想將它們連接起來得到一個像這樣的二維數組:
A = [B1, B2, B3]
在哪里
B1 = np.concatenate((A1,A2, A3),axis=1)
B2 = np.concatenate((A4,A5, A6),axis=1)
B3 = np.concatenate((A7,A8, A9),axis=1)
在我的情況下,我將有 N arrays ,我像這樣計算 N 的值:
img = Image.open(file_name)
img_width, img_height = img.size
tile_height = int(input('Enter the height of tile:'))
tile_width = int(input("Enter the width of tile:'))
N = (img_height//tile_height)*(img_width//tile_width)
# **The image will be broken down into n tiles of size tile_width x tile_height**
for i in range(img_height//tile_height):
for j in range(img_width//tile_width):
box = (j*width, i*height, (j+1)*width, (i+1)*height)
img.crop(box)
...
所以基本上,我有一個圖像被分解成 N 個圖塊,經過一些處理,我將這些圖像圖塊數據存儲為 numpy arrays 並且我想將它們連接/合並成一個 2D Z2EA9510C37F7F821ECBFZ1 數組中的原始二維 Z2EA9510C37F7F89ECBFZ1 數組圖片。 我怎樣才能做到這一點?
這似乎是bmat的完美用例
編輯:如何使用 bmat
bmat 接受塊矩陣作為第一個參數。
[[A11, A12, ..., A1n]
[A21, A22, ..., A2n]
...
[Am1, Am2, ..., Amn]]
並且不限於 9 個子矩陣的情況,巧合的是,在bmat
文檔中,它們的示例與您問題中的示例大小相同。
import numpy as np;
mats = []
for i in range(10):
mats.append(np.ones((4, 2)) * i);
np.bmat([mats[:5], mats[5:]])
給
matrix([[0., 0., 1., 1., 2., 2., 3., 3., 4., 4.],
[0., 0., 1., 1., 2., 2., 3., 3., 4., 4.],
[0., 0., 1., 1., 2., 2., 3., 3., 4., 4.],
[0., 0., 1., 1., 2., 2., 3., 3., 4., 4.],
[5., 5., 6., 6., 7., 7., 8., 8., 9., 9.],
[5., 5., 6., 6., 7., 7., 8., 8., 9., 9.],
[5., 5., 6., 6., 7., 7., 8., 8., 9., 9.],
[5., 5., 6., 6., 7., 7., 8., 8., 9., 9.]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.