Generate 6x6 array and compute Matrix rank with NumPy
NumPy: Advanced Exercise-23 with Solution
Write a NumPy program to create a 6x6 array with random values and compute the rank of the matrix.
This NumPy program generates a 6x6 array with random values and then computes the rank of the matrix. The rank of a matrix is the maximum number of linearly independent row or column vectors in the matrix.
Sample Solution:
Python Code:
# Importing the necessary NumPy library
import numpy as np
# Create a 6x6 array with random values
array = np.random.rand(6, 6)
# Compute the rank of the matrix
rank = np.linalg.matrix_rank(array)
# Printing the original array and its rank
print("6x6 Array:\n", array)
print("Rank of the array:\n", rank)
Output:
6x6 Array: [[0.35821884 0.24951293 0.4067254 0.69948259 0.56775495 0.43700624] [0.1970947 0.42463218 0.02494649 0.7973269 0.49270272 0.09471064] [0.4135473 0.68226842 0.79785926 0.08811048 0.05472666 0.87031629] [0.99204677 0.56798758 0.95294414 0.23025108 0.16168588 0.34648636] [0.65915128 0.23049596 0.14487972 0.98587358 0.7798649 0.03003929] [0.16702194 0.29662257 0.77849454 0.07513274 0.5119075 0.91652833]] Rank of the array: 6
Explanation:
- Import NumPy library: This step imports the NumPy library, which is essential for numerical operations.
- Create a 6x6 array: We use np.random.rand(6, 6) to generate a 6x6 matrix with random values between 0 and 1.
- Compute the rank of the matrix: The np.linalg.matrix_rank function calculates the rank of the given matrix.
- Print results: This step prints the original array and its rank.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Extract the lower triangular part of a 4x4 Random array using NumPy.
Next: Compute the Frobenius norm of a 3x3 random array using NumPy.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics