w3resource

NumPy: Add two arrays A and B of sizes (3,3) and (,3)

NumPy: Array Object Exercise-146 with Solution

Write a NumPy program to add two arrays A and B of sizes (3,3) and (,3).

Pictorial Presentation:

NumPy: Add two arrays A and B of sizes (3,3) and (,3)

Sample Solution:

Python Code:

import numpy as np
A = np.ones((3,3))
B = np.arange(3)
print("Original array:")
print("Array-1")
print(A)
print("Array-2")
print(B)
print("A + B:")
new_array = A + B
print(new_array)

Sample Output:

Original array:
Array-1
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
Array-2
[0 1 2]
A + B:
[[1. 2. 3.]
 [1. 2. 3.]
 [1. 2. 3.]]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to extract first, third and fifth elements of the third and fifth rows from a given (6x6) array.
Next: Write a NumPy program to create an array that represents the rank of each item of a given array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]