使用列创建矩阵| 使用Python的线性代数

In the python code, we will build a matrix using columns. We can stack two arrays to form a matrix only and only if both the column vectors have the same dimensions. The following code shows how an inbuilt hstack() function can be used to stack three columns to form a matrix.

在python代码中,我们将使用列来构建矩阵。 仅当两个列向量具有相同的维数时,我们才可以堆叠两个数组以形成矩阵。 以下代码显示了如何使用内置的hstack()函数来堆叠三列以形成矩阵。

使用列创建矩阵的Python代码 (Python code for creating a matrix using columns)

# Creating a Matrix using Columnsn

import numpy as np

#Use of np.array() to define columns
V1 = np.array([[45],[78],[65],[99]])
V2 = np.array([[68],[87],[97],[48]])
V3 = np.array([[74],[68],[77],[48]])


print("The column v1: ",V1)
print("The column v2: ",V2)
print("The column v3: ",V3)

#Making a Matrix using Vectors
M = np.hstack([V1,V2,V3])

print("\n\n -----Matrix M---\n", M)
print("\n\nShape of the matrix B: ", M.shape)

Output:

输出:

The column v1:  [[45]
 [78]
 [65]
 [99]]
The column v2:  [[68]
 [87]
 [97]
 [48]]
The column v3:  [[74]
 [68]
 [77]
 [48]]


 -----Matrix M---
 [[45 68 74]
 [78 87 68]
 [65 97 77]
 [99 48 48]]


Shape of the matrix B:  (4, 3)


翻译自: https://www.includehelp.com/python/creating-a-matrix-using-columns.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值