如何使在numpy的阵列的基质? [英] How to make a matrix of arrays in numpy?

查看:112
本文介绍了如何使在numpy的阵列的基质?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个2×2矩阵

T = [[A, B],
     [C, D]]

,其中每个元素 A,B,C,D 是(当然是相同的大小,)数组。这可能吗?

where each element A,B,C,D is an array (of same size, of course). Is this possible?

我希望能够繁殖这些基质,例如乘上两类​​矩阵 T1 T2 应该给我

I would like to be able to multiply these matrix, for example multiplying two matrix T1 and T2 should give me

T1*T2 = [[A1*A2, B1*B2],
         [C1*C2, D1*D2]]

这仍然是相同大小的阵列的矩阵。有这样的乘法函数?

which is still a matrix of arrays of the same size. Is there such a multiplication function?

而且,如果我乘 T 与正常的标量矩阵 T = [[A,B],[C,D] ,其中 A,b,C,D 是标量数字,乘法应该给我

And also, if I multiply T with a normal scalar matrix t = [[a,b],[c,d]] where a,b,c,d are scalar numbers, the the multiplication should give me

t*T = [[a*A, b*B],
       [c*C, d*D]]

我怎样才能做到这一点?一个例子或相关材料的链接将是巨大的。

How can I do this? An example or a link to related material would be great.

推荐答案

不你的第一个问题,只是工作如你所愿?

Doesn't your first question just work as you would expect?

In [1]: import numpy as np

In [2]: arr = np.arange(8).reshape(2, 2, 2)

In [3]: arr
Out[3]: 
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

In [4]: arr*arr
Out[4]: 
array([[[ 0,  1],
        [ 4,  9]],

       [[16, 25],
        [36, 49]]])


至于你的第二个问题,只是把它重塑到3维数组:


As for your second question, just reshape it to a 3 dimensional array:

In [5]: arr2 = np.arange(4).reshape(2, 2)

In [6]: arr2
Out[6]: 
array([[0, 1],
       [2, 3]])

In [7]: arr2 = arr2.reshape(2, 2, 1)

In [8]: arr2
Out[8]: 
array([[[0],
        [1]],

       [[2],
        [3]]])

In [9]: arr*arr2
Out[9]: 
array([[[ 0,  0],
        [ 2,  3]],

       [[ 8, 10],
        [18, 21]]])

这篇关于如何使在numpy的阵列的基质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆