絕對使用來自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]]
>>>
我想如果你解釋了你想要對矩陣中的每一行/元素做什麼,你會發現你已經描述了算法,並且可以在Python中自己實現。 – 2011-04-26 04:50:42
這將有助於:http://stackoverflow.com/questions/5586719/scipy-interpolation-how-to-resize-resample-3x3-matrix-to-5x5 – carl 2011-04-26 05:04:19