Calling Row of a Matrix using Function | Linear Algebra using Python

Linear Algebra using Python | Calling Row of a Matrix using Function: Here, we are going to learn how to call row of a matrix using function in Python?
Submitted by Anuj Singh, on May 21, 2020

Prerequisite: Linear Algebra | Defining a Matrix

Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. Matrix is the key to linear algebra.

All the linear algebra revolves around matrices. Row operations are one of the key steps in many Linear Algebra methods(we will see in future tutorials). Therefore, whenever we have to go for Row operations, then we have to call our Row. A function provides a reproducibility feature to the program and therefore it is good to build a function for calling a desired Row.

Python code for Calling Row of a Matrix using Function

# Linear Algebra Learning Sequence
# Calling Row of a Matrix using Function

import numpy as np

# Defining function for calling row of matrix    
def row(M,r):
    rowi = M[r-1]
    return rowi

# Use of np.array() to define a matrix
V = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])
print("--The Matrix-- \n",V)

# input row number
i = int(input("Enter i (row number) : "))

# getting row
row = row(V,i)

# printing row
print("Row ",i,':', row)

Output:

RUN 1:
--The Matrix-- 
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]
Enter i (row number) : 1
Row  1 : [1 2 3]

RUN 2:
--The Matrix-- 
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]
Enter i (row number) : 4
Row  4 : [323 623 823]



Comments and Discussions!

Load comments ↻






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