如何在numpy中制作矩阵向量

发布于 2025-01-26 17:34:37 字数 224 浏览 0 评论 0原文

我想在numpy中创建一个相同矩阵的向量(作为数组)。假设矩阵是:

w = np.array([[1,2],
              [3,4],
              [5,6]])

那么,如何在每个位置中使用矩阵创建一个固定长度的向量?

那就是:vector [0] = ... = vector [n] = w

I would like to create a vector of the same matrix in numpy (so as an array). Let's say the matrix is:

w = np.array([[1,2],
              [3,4],
              [5,6]])

Then, how can I create a vector of a fixed length with the matrix w in every position?

That is: vector[0] = ... = vector[n] = w

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-02-02 17:34:37

不确定要确切的预期输出,要创建一个额外的维度,您可以使用 numpy.tile

n = 3
vector = np.tile(w, (n, 1, 1))

nb。 vector实际上不是(1D)向量,而是3D数组

输出:

# vector
array([[[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]]])

# vector[0]
array([[1, 2],
       [3, 4],
       [5, 6]])

Not sure of the exact expected output, to to create an additional dimension you could use numpy.tile:

n = 3
vector = np.tile(w, (n, 1, 1))

NB. vector is not really a (1D) vector it is a 3D array

output:

# vector
array([[[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]],

       [[1, 2],
        [3, 4],
        [5, 6]]])

# vector[0]
array([[1, 2],
       [3, 4],
       [5, 6]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文