What is the process for multiplying arrays of different dimensions?

This question came to my mind as I was going through this discussion on this platform. I tried the same code snippets on my own personal computer but tried multiplying arrays of different dimensions but it didn’t work. Logically, it shouldn’t as matrices with different dimensions do not multiply in some cases (eg. a matrix of 2x3 and 4x5). But I was still curious, is there any way of any sort that I can maybe make them multiply each other? Also, please do provide a code snippet with your replies, for clarification.

1 Like

Yes, there is a way by which you can multiply matrices of different shapes and the method is known as Broadcasting. I am attaching below an example code that implements this technique on two matrices of different shapes:

  • This code uses broadcasting to multiply matrices a and b along the last axis, resulting in a new array c with dimensions (5, 5, 3).
  • The resulting shape of c indicates that the last axis of b was broadcasted (repeated) to match the third axis of a.