2011-04-26 47 views
3

我是python中的新成員,我對數組/矩陣有個疑問。 下面是我得到的矩陣。如何在python中重新取樣數組

A =

[[85 77 83 ...,59 58 59]

[80 83 80 ...,57 60 58]

[75 76 81。 ..,59 58 60]

我想重樣(我不知道這是否是正確的字)的矩陣,使其成爲

B =

[[85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[85 85 85 85 77 77 77 77 83 83 83 83 ..... 59 59 59 59 58 58 58 58 59 59 59 59]

[85 85 85 85 77 77 77 77 83 83 83 83 ... ... 59 59 59 59 58 58 58 58 59 59 59 59]

[85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[80 80 80 80 83 83 83 83 80 80 80 80 57 ....... 57 57 57 60 60 60 60 58 58 58 58]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]]

我在網上搜索,看了很多帖子,但仍然沒有線索如何做到這一點。 所以請教我如何做到這一點,我非常感謝。

+0

我想如果你解釋了你想要對矩陣中的每一行/元素做什麼,你會發現你已經描述了算法,並且可以在Python中自己實現。 – 2011-04-26 04:50:42

+0

這將有助於:http://stackoverflow.com/questions/5586719/scipy-interpolation-how-to-resize-resample-3x3-matrix-to-5x5 – carl 2011-04-26 05:04:19

回答

2

以下實現遞歸地重新抽樣(重複)包含數字的矩陣或列表(任何可迭代的容器類型)(任何不可迭代的對象)。理解起來比其他選擇更快更簡單。它可以處理任意嵌套的列表。每個子列表都被深度複製。

import itertools 

def resample(obj, n): 
    try: 
     return list(itertools.chain.from_iterable((resample(row, n) for c in xrange(n)) for row in obj)) 
    except TypeError: 
     return obj 

用法:

>>> l = [1, 2, 3, 4] 
>>> resample(l, 4) 
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4] 

>>> m = [[1, 2, 3, 4], [5, 6, 7, 8]] 
>>> resample(m, 4) 
[[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4], 
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4], 
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4], 
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4], 
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8], 
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8], 
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8], 
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8]] 
3

絕對使用來自Scipy interpolation how to resize/resample 3x3 matrix to 5x5?的信息。

但我覺得我身邊倒是很亂,這裏就是我的了:

可能是有史以來最糟糕的尋找方法:

>>> import pprint 
>>> a = [[85, 77, 99], 
...  [11, 22, 33], 
...  [44, 55, 66]] 
>>> 
>>> def transform(n,matrix): 
...  return [item for sublist in [[[item for sublist in [[element]*n for element in row] for item in sublist] for _ in range(n)] for row in matrix] for item in sublist] 
... 
>>> pprint.pprint(transform(3,a)) 
[[85, 85, 85, 77, 77, 77, 99, 99, 99], 
[85, 85, 85, 77, 77, 77, 99, 99, 99], 
[85, 85, 85, 77, 77, 77, 99, 99, 99], 
[11, 11, 11, 22, 22, 22, 33, 33, 33], 
[11, 11, 11, 22, 22, 22, 33, 33, 33], 
[11, 11, 11, 22, 22, 22, 33, 33, 33], 
[44, 44, 44, 55, 55, 55, 66, 66, 66], 
[44, 44, 44, 55, 55, 55, 66, 66, 66], 
[44, 44, 44, 55, 55, 55, 66, 66, 66]] 
>>> pprint.pprint(transform(4,a)) 
[[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99], 
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99], 
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99], 
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99], 
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33], 
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33], 
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33], 
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33], 
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66], 
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66], 
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66], 
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66]] 
>>> pprint.pprint(transform(5,a)) 
[[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99], 
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99], 
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99], 
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99], 
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99], 
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33], 
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33], 
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33], 
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33], 
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33], 
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66], 
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66], 
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66], 
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66], 
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66]] 
>>> 
0

我沒有完全理解你的算法或你想要什麼這樣做,但是:

a=[[1,2],[3,4]] 
# grow horizontally, 5 times 
b=[[c for d in zip(x,x,x,x,x) for c in d] for x in a] 
# grow vertically, 5 times 
c= [z[:] for x in ((y[:],y[:],y[:],y[:],y[:]) for y in b) for z in x] 

注意,它與任何事物陣列,因爲它僅使用基本語言基本

0
import numpy as np 
import scipy as sp 

# your matrix. Let's say A(3,3) with random values from 0 to 20 
A = sp.random.randint(20,size=(3,3)) 

# Resize as you want (m x n) 
m =5 
n =5 
New_A = sp.kron(A, sp.ones(m,n)) 

print New_A 

即使這是一個有點晚了我更願意給一些關於它的評論答案!