Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 6
added 624 characters in body
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 17 bytes

XJiq:"!J21ドル!*s1$e

Try it online!

This avoids the matrix multiplication doing it manually, by means of element-wise multiplication with broadcast. Specifically, to multiply matrices A and B, both of size n×ばつn:

  1. Permute the dimensions of B such that B is "turned" with a rotation axis along the first dimension, giving an n×ばつ1_n_ 3D array, say C.
  2. Multiply each element of A times each element of C, with implicit broadcast. This means that A is automatically replicated n times along the third dimension, and C is replicated n times along the second, to make them both n×ばつn×ばつn before the actual element-wise multiplication
  3. Sum along the first dimension to yield a ×ばつn×ばつn array.
  4. Squeeze the leading singleton dimension out, to produce an n×ばつn result.
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

AltStyle によって変換されたページ (->オリジナル) /