Q:

Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix

0

Write a NumPy program to extract rows with unequal values (e.g. [1,1,2]) from 10x3 matrix.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
nums = np.random.randint(0,4,(6,3))
print("Original vector:")
print(nums)
new_nums = np.logical_and.reduce(nums[:,1:] == nums[:,:-1], axis=1)
result = nums[~new_nums]
print("\nRows with unequal values:")
print(result)

Sample Output:

Original vector:
[[3 2 0]
 [2 3 1]
 [2 0 3]
 [3 3 1]
 [2 1 1]
 [3 0 2]]

Rows with unequal values:
[[3 2 0]
 [2 3 1]
 [2 0 3]
 [3 3 1]
 [2 1 1]
 [3 0 2]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now