数字块没有作为Matlab repm工作

2024-04-25 21:40:54 发布

您现在位置:Python中文网/ 问答频道 /正文

根据What is the equivalent of MATLAB's repmat in NumPy,我试图使用python从3x3数组构建3x3x5数组。在

在Matlab中,这一切如我所料。在

a = [1,1,1;1,2,1;1,1,1];
a_= repmat(a,[1,1,5]);

尺寸(a_u)=3 3 5

但是为了数字块在

^{pr2}$

形状=(1,3,15)

如果我想生成与Matlab中相同的数组,等价的是什么?在

编辑1

我期望得到的输出是

b_(:,:,1) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,2) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,3) =

1 1 1  
1 2 1  
1 1 1  

b_(:,:,4) =  

1 1 1  
1 2 1  
1 1 1  
b_(:,:,5) =

1 1 1  
1 2 1  
1 1 1  

但是@farenorth和numpy.dstack给出的是什么

[[[1 1 1 1 1]  
[1 1 1 1 1]  
[1 1 1 1 1]]  

[[1 1 1 1 1]  
[2 2 2 2 2]  
[1 1 1 1 1]]  

[[1 1 1 1 1]  
[1 1 1 1 1]  
[1 1 1 1 1]]]  

Tags: oftheinnumpyis尺寸数字数组
2条回答

通常,NumPy函数不是matlab函数的“插入式”替换。通常情况下,“等价”函数的使用方式有细微差别。适应确实需要时间,但我发现这种转变是非常值得的。在

在本例中,np.tile文档指出当您试图将数组平铺到比定义的更高的维时会发生什么情况

numpy.tile(A, reps)

Construct an array by repeating A the number of times given by reps.

If reps has length d, the result will have dimension of max(d, A.ndim).

If A.ndim < d, A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.

在本例中,数组被转换为[1, 3, 3]的形状,然后平铺。因此,要获得您想要的行为,只需确保在数组中添加一个新的singleton维度

>>> b_ = numpy.tile(b[..., None], [1, 1, 5])
>>> print(b_.shape)
(3, 3, 5)

注意这里我使用了None(即np.newaxis)和省略号符号在数组的末尾指定一个新的维度。您可以找到有关这些功能的更多信息here。在

另一个选择,这是受OP评论启发的:

^{pr2}$

{a2'在这种情况下由^'数组^'构造。在

正如@hpaulj指出的,Matlab和NumPy显示矩阵的方式不同。要复制Matlab输出,可以执行以下操作:

>>> for idx in xrange(b_.shape[2]):
...    print 'b_[:, :, {}] = \n{}\n'.format(idx, str(b_[:, :, idx]))
...
b_[:, :, 0] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 1] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 2] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 3] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

b_[:, :, 4] = 
[[1 1 1]
 [1 2 1]
 [1 1 1]]

祝你好运!在

让我们试试比较,注意使形状和价值多样化。在

octave:7> a=reshape(0:11,3,4)
a =
    0    3    6    9
    1    4    7   10
    2    5    8   11

octave:8> repmat(a,[1,1,2])
ans =
ans(:,:,1) =
    0    3    6    9
    1    4    7   10
    2    5    8   11
ans(:,:,2) =    
    0    3    6    9
    1    4    7   10
    2    5    8   11

numpy等价物-或多或少:

^{pr2}$

再次numpy,但是使用order F来更好地匹配matlabfortran派生的布局

In [63]: a=np.arange(12).reshape(3,4,order='F')    
In [64]: np.tile(a,[2,1,1])
Out[64]: 
array([[[ 0,  3,  6,  9],
        [ 1,  4,  7, 10],
        [ 2,  5,  8, 11]],

       [[ 0,  3,  6,  9],
        [ 1,  4,  7, 10],
        [ 2,  5,  8, 11]]])

我在开始时添加了新的numpy维度,因为在很多方面它可以更好地复制MATLAB在结尾添加它的实践。在

尝试在末尾添加新维度。形状是(3,4,5),但您可能不喜欢显示。在

 np.tile(a[:,:,None],[1,1,2])

另一个需要考虑的问题是,当你把瓷砖弄平后会发生什么?在

octave:10> repmat(a,[1,1,2])(:).'
ans =    
    0    1    2    3    4    5    6    7    8    9   10   11 
0    1    2    3     4    5    6    7    8    9   10   11

顺序为Fa

In [78]: np.tile(a[:,:,None],[1,1,2]).flatten()
Out[78]: 
array([ 0,  0,  3,  3,  6,  6,  9,  9,  1,  1,  4,  4,  7,  7, 10, 10,  2,
        2,  5,  5,  8,  8, 11, 11])

In [79]: np.tile(a,[2,1,1]).flatten()
Out[79]: 
array([ 0,  3,  6,  9,  1,  4,  7, 10,  2,  5,  8, 11,  0,  3,  6,  9,  1,
        4,  7, 10,  2,  5,  8, 11])

使用C顺序数组:

In [80]: a=np.arange(12).reshape(3,4)

In [81]: np.tile(a,[2,1,1]).flatten()
Out[81]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,  0,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11])

最后一个匹配八度音阶布局。在

也是这样:

In [83]: a=np.arange(12).reshape(3,4,order='F')

In [84]: np.tile(a[:,:,None],[1,1,2]).flatten(order='F')
Out[84]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,  0,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11])

困惑了吗?在

相关问题 更多 >