0

I have a 2d array and a 1d array and I need to multiply each element in the 1d array x each element in the 2d array columns. It's basically a matrix multiplication but numpy won't allow matrix multiplication because of the 1d array. This is because matrices are inherently 2d in numpy. How can I get around this problem? This is an example of what I want:

FrMtx = np.zeros(shape=(24,24)) #2d array
elem = np.zeros(24, dtype=float) #1d array
Result = np.zeros(shape=(24,24), dtype=float) #2d array to store results

some_loop to increment i:
    some_other_loop to increment j:
        Result[i][j] = (FrMtx[i][j] x elem[j])

Numerous efforts have given me errors such as arrays used as indices must be of integer or boolean type

4
  • As written, Result is a 1d array, not a 2d array. Is it supposed to have shape (24, 24)? Commented Jul 12, 2012 at 21:23
  • @JoshBleecherSnyder yes that's correct sorry will edit now. I think Sven has managed to sort it for me anyway though Commented Jul 12, 2012 at 21:25
  • Yep. Sven's answer works. For future reference, if you need to do something broadcasting won't handle, you can work on slices of arrays like Result[:, j] or Result[i, :]... Commented Jul 12, 2012 at 21:26
  • @JoshBleecherSnyder thank you that's very helpful. I'm still getting to grips with python/numpy Commented Jul 12, 2012 at 21:30

2 Answers 2

4

Due to the NumPy broadcasting rules, a simple

Result = FrMtx * elem

Will give the desired result.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect Sven. I had been trying to use the dot function and it wasn't working. Thank you.
0

You should be able to just multiply your arrays together, but its not immediately obvious what 'direction' the arrays will be multiplied since the matrix is square. To be more explicit about which axes are being multiplied, I find it is helpful to always multiply arrays that have the same number of dimensions.

For example, to multiply the columns:

mtx = np.zeros(shape=(5,7))
col = np.zeros(shape=(5,))
result = mtx * col.reshape((5, 1))  

By reshaping col to (5,1), we guarantee that axis 0 of mtx is multiplied against axis 0 of col. To multiply rows:

mtx = np.zeros(shape=(5,7))
row = np.zeros(shape=(7,))
result = mtx * row.reshape((1, 7))

This guarantees that axis 1 in mtx is multiplied by axis 0 in row.

2 Comments

Sorry for the late reply but isn't matrix multiplication inherently the rows of x multiplied by the columns of y, thereby not needing to specify the direction of multiplication?
That is the convention for matrix multiplication in linear algebra. Array multiplication in numpy is not at all the same--whereas matrix multiplication involves sums of products, numpy array multiplication is a simple elementwise product where the elements that get multiplied together are decided based on numpy's broadcasting rules. ( docs.scipy.org/doc/numpy/user/basics.broadcasting.html ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.