in Education by
This question already has answers here: Forming a matrix by summing 2 arrays element-array wise using NumPy (2 answers) Closed 3 years ago. Given 2 numpy (1D) arrays, I want to compute the 2D array containing all possible sums of elements of those two arrays. For example, like this: import numpy as np v = np.array([1, 2, 3]) w = np.array([4, 5, 6]) sum_matrix = np.zeros((3,3)) for i in range(3): for j in range(3): sum_matrix[i,j] = v[i] + w[j] print(sum_matrix) This returns the correct answer [[5. 6. 7.] [6. 7. 8.] [7. 8. 9.]] but I want to avoid python loops (and certainly a double one). Is there some nice numpy functionality for this situation? I can think of one solution that avoids all loops, but it feels kind of like a hack. I noticed that if v is a column matrix and w a row matrix, then the matrix product v @ w returns the matrix of all possible products. I can translate this to sums using log and exp: v = v[np.newaxis].T # Turn v into a column matrix w = w[np.newaxis] # Turn w into a row matrix print(np.log(np.exp(v) @ np.exp(w))) This prints the same as the above. Question: Are there other, less hacky, numpy solutions to this problem? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use np.add.outer, which will be applying np.add on each value in v and w: np.add.outer(v,w) array([[5, 6, 7], [6, 7, 8], [7, 8, 9]])

Related questions

0 votes
    This question already has answers here: Forming a matrix by summing 2 arrays element-array wise using NumPy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I want to test a 2x2 matrix of [[5,6],[7,8]] to see if it's a square. I run ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to multiply these two matrices using pure python. Input (M1 is 3*3 and Mt is a 3*2) M1 = ... But technically this should work. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: asynchronous python itertools chain multiple generators (2 answers) Closed 2 ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How do I plot two countplot graphs side by side in seaborn? ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How do I plot two countplot graphs side by side in seaborn? ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I've always laughed to myself when I've looked back at my VB6 days and thought, "What modern language doesn't ... ++/-- notation? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Is there a way to get functionality similar to mkdir -p on the shell from within Python. I am looking for a ... has already written it? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I had some Python 2 code as follows (pardon the indentation): def getZacksRating(symbol): c = httplib.HTTPSConnection("www. ... data.split(' ')[1] result = ratingPart.partition("...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Currently I try to switch from vim to emacs and have some difficulty while editing code. For most of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    This is currently how I copy code from an editor to a search field in VS code using vscodevim. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
...