检查平方矩阵 使用Python的线性代数

Prerequisite: Linear Algebra | Defining a Matrix

先决条件: 线性代数| 定义矩阵

Square Matrices are one of the important Matrices and therefore, we are going to write a python code to check whether the matrix is square or not?

平方矩阵是重要的矩阵之一,因此,我们将编写一个python代码来检查矩阵是否为平方?

用于检查方矩阵的Python代码 (Python code for checking square matrix)

# Linear Algebra Learning Sequence
# Checking Square Matrix

import numpy as np

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

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

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

print("\n\n-----Matrix M---\n", M)

[r,c] = M.shape
if r == c:
    print("Matrix is square with dim ",r,'x',r )
else:
    print("Matrix is not square with dim ",r,'x',c )

V1 = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])
V2 = np.array([[965,2413,78],[223,356,500],[312,66,78]])

[r2,c2] = V1.shape
[r3,c3] = V2.shape
print('\n\n----Matrix A----\n',V1)
if r2 == c2:
    print("Matrix is square with dim ",r2,'x',r2 )
else:
    print("Matrix is not square with dim ",r2,'x',c2 )

print('\n\n----Matrix B----\n',V2)
if r3 == c3:
    print("Matrix is square with dim ",r3,'x',r3 )
else:
    print("Matrix is not square with dim ",r3,'x',c3 )

Output:

输出:

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


-----Matrix M---
 [[45 78 65 99]
 [68 87 97 48]
 [74 68 77 48]]
Matrix is not square with dim  3 x 4


----Matrix A----
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]
Matrix is not square with dim  4 x 3


----Matrix B----
 [[ 965 2413   78]
 [ 223  356  500]
 [ 312   66   78]]
Matrix is square with dim  3 x 3


翻译自: https://www.includehelp.com/python/checking-square-matrix.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值