0

I am trying to solve below practice question and stuck.

Q> Write a function called operations that takes as input two positive integers h and w, makes two random matrices A and B, of size h x w, and returns A,B, and s, the sum of A and B.

import numpy as np
def operations(h, w):
    if h>0 and w>0 and type(h)==int and type(w)==int:
        A = np.random.rand(h, w)
        B = np.random.rand(h, w)
        s = (A, B, A+B)
        return A
        return B
        return s
x=int(input("Enter a positive number for x: "))
y=int(input("Enter a positive number for y: "))
r=operations(x, y)
print(r)
Ian Chung
  • 21
  • 6
  • 1
    Does this answer your question? [How do I return multiple values from a function?](https://stackoverflow.com/questions/354883/how-do-i-return-multiple-values-from-a-function) – Ken Y-N Feb 17 '20 at 04:17

1 Answers1

0
import numpy as np
def operations(h, w):
    if h>0 and w>0 and type(h)==int and type(w)==int:
        A = np.random.rand(h, w)
        B = np.random.rand(h, w)
        s = A+B
        return A,B,s
x=int(input("Enter a positive number for x: "))
y=int(input("Enter a positive number for y: "))
A,B,s=operations(x, y)
print('A = ')
print(A)
print('B = ')
print(B)
print('Sum A and B = ')
print(s)
Aly Hosny
  • 827
  • 5
  • 13
  • thanks for your help but not sure your suggestion is correct. Not sure im getting that ARRAY text in the answer. ''' Enter a positive number for x: 2 Enter a positive number for y: 3 (array([[0.4676197 , 0.0919708 , 0.55713748], [0.30095933, 0.71896994, 0.97515057]]), array([[0.80497518, 0.87438986, 0.24890498], [0.78458548, 0.15510706, 0.74411273]]), array([[1.27259488, 0.96636066, 0.80604246], [1.08554481, 0.874077 , 1.71926329]])) ''' – Ian Chung Feb 17 '20 at 15:05
  • it is a 2d array, i.e a matrix if you want it to be called a matrix "which makes no difference here" you can use np.asmatrix(np.random.rand(h, w)) – Aly Hosny Feb 18 '20 at 07:50