I'm trying to multiply some arrays together but can't seem to figure out how to do it. I'm translating some linear algebra code from MatLab and can't seem to get it to work the same in Numpy due to Matlab using column-major indexing and Python using row-major indexing.
I've managed to get the matrices to multiply, but I've not managed to get the same result as the one in Matlab.
I have three arrays:
a.shape = 40x40 in Python, 40x40 in Matlab, zeroes array
b.shape = 40x21 in Python, 21x40 in Matlab, array with < 1 float values
c.shape = 31x40 in Python, 40x31 in Matlab, array with < 1 float values
The math I'm trying to copy from Matlab is:
D = b*(a*c);
disp(size(D)); % Size of D is 21x31
When I try and do the same with NumPy:
D = b @ (a @ c)
It obviously doesn't work since c is 31x40 and can't multiply with A (40x40).
I've managed to get the multiplication to actually work by using:
D = np.transpose(np.transpose(b) @ (a @ np.transpose(c)))
but the resulting D in Numpy is different from the one in Matlab, although the dimensions are correct (31x21).
If anyone has any ideas how to do this or even if it's not possible please let me know!
a
as well to get the same output. And if you're transposing everything, maybe you are defining your matrices wrong. You should preserve the shape they have in MATLAB, as MadPhysicist suggests. – Cris Luengoc@a@b
should produce a (31,21). Think A@B last of A pairs with 2nd to the last of B – hpaulj