Shape of Matrix | Linear Algebra using Python

Linear Algebra using Python | Shape of Matrix: Here, we are going to learn how to find shape of matrix in Python?
Submitted by Anuj Singh, on May 20, 2020

Prerequisite: Linear Algebra | Defining a Matrix

In the python code, we will add two Matrices. We can add two Matrices only and only if both the matrices have the same dimensions. Therefore, knowing the dimensions of the matrices turns out to be one of the major steps in Linear Algebra. The following code shows how an inbuilt function can be used to achieve our goal of the shape of a Matrix.

Python code for fidning Shape of Matrix

# Linear Algebra Learning Sequence
# Shape of Matrix 

import numpy as np

#Use of np.array() to define a matrix
V1 = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])
V2 = np.array([[965,2413,78,44],[223,356,500,44],[312,66,78,44],[42,42,42,44],[44,44,44,44]])

print("--The Matrixa A-- \n",V1)
print("\n--The Matrix B-- \n",V2)

print("\n\n Shape of the matrix A: ", V1.shape)
print(" Shape of the matrix B: ", V2.shape)

Output:

--The Matrixa A-- 
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]

--The Matrix B-- 
 [[ 965 2413   78   44]
 [ 223  356  500   44]
 [ 312   66   78   44]
 [  42   42   42   44]
 [  44   44   44   44]]


 Shape of the matrix A:  (4, 3)
 Shape of the matrix B:  (5, 4)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.