以特定方式重塑Python数组

我正在研究Python代码,以便基本上可以使用Numpy Array做到这一点。

http://img4.mukewang.com/6070123c00017f2112940864.jpg

我有一个Matlab代码,这是


A = [1:30]'; % Example matrix

rows = 3;


for i=1:(numel(A)-rows+1)

    B(1:rows,i)=A(i:i+rows-1,1);

end

或者,没有任何循环,


B = conv2(A.', flip(eye(rows)));

B = B(:, rows:end-rows+1);


有人可以帮我在Python中做同样的事情吗?使用重塑功能无济于事,因为我需要“镜像”值(而不仅仅是重组它们)。


慕的地8271018
浏览 146回答 3
3回答

吃鸡游戏

使用np.ndarray.reshape:import numpy as npA = np.arange(1, 31)B = A.reshape((3, 10))print(B)[[ 1  2  3  4  5  6  7  8  9 10] [11 12 13 14 15 16 17 18 19 20] [21 22 23 24 25 26 27 28 29 30]]

慕仙森

尝试该代码段:import numpy as npstart = 1end = 30b_dim = 28a = np.arange(start, end+1)b = np.zeros((3, b_dim))print("a = ", a)rows, _ = b.shapefor row in range(rows):    data = a[row:row+b_dim]    b[row, :] = dataprint("b = ", b)它打印('a = ', array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]))('b = ', array([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,         12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,         23.,  24.,  25.,  26.,  27.,  28.],       [  2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,         13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.,         24.,  25.,  26.,  27.,  28.,  29.],       [  3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,         14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.,  24.,         25.,  26.,  27.,  28.,  29.,  30.]]))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python