What does matrix multiplication mean? Here's a few common intuitions:

**1) Matrix multiplication scales/rotates/skews a geometric plane.**

This is useful when first learning about vectors: vectors go in, new ones come out. Unfortunately, this can lead to an over-reliance on geometric visualization.

If 20 families are coming to your BBQ, how do you estimate the hotdogs you need? (*Hrm… 20 families, call it 3 people per family, 2 hotdogs each… about 20 * 3 * 2 = 120 hotdogs.*)

You probably don't think "Oh, I need the volume of a invitation-familysize-hunger prism!". With large matrices I don't think about 500-dimensional vectors, just data to be modified.

**2) Matrix multiplication composes linear operations.**

This is the technically accurate definition: yes, matrix multiplication results in a new matrix that composes the original functions. However, sometimes the matrix being operated on is not a linear operation, but a set of vectors or data points. We need another intuition for what's happening.

I'll put a programmer's viewpoint into the ring:

**3) Matrix multiplication is about information flow, converting data to code and back.**

I think of linear algebra as "math spreadsheets" (if you're new to linear algebra, read this intro):

- We store information in various spreadsheets ("matrices")
- Some of the data are seen as functions to apply, others as data points to use
- We can swap between the vector and function interpretation as needed

Sometimes I'll think of data as geometric vectors, and sometimes I'll see a matrix as a composing functions. But mostly I think about information flowing through a system. (Some purists cringe at reducing beuatiful algebraic structures into frumpy spreadsheets; I sleep OK at night.)

## Programmer's Intuition: Code is Data is Code

Take your favorite recipe. If you interpret the words as *instructions*, you'll end up with a pie, muffin, cake, etc.

If you interpret the words as *data*, the text is prose that can be tweaked:

- Convert measurements to metric units
- Swap ingredients due to allergies
- Adjust for altitude or different equipment

The result is a new recipe, which can be further tweaked, or executed as instructions to make a different pie, muffin, cake, etc. (Compilers treat a program as text, modify it, and eventually output "instructions" — which could be text for another layer.)

That's Linear Algebra. We take raw information like "3 4 5" treat it as a vector or function, depending on how it's written:

By convention, a vertical column is usually a vector, and a horizontal row is typically a function:

`[3; 4; 5]`

means`x = (3, 4, 5)`

. Here,`x`

is a vector of data (I'm using`;`

to separate each row).`[3 4 5]`

means`f(a, b, c) = 3a + 4b + 5c`

. This is a function taking three inputs and returning a single result.

And the aha! moment: data is code, code is data!

The row containing a horizontal function could really be three data points (each with a single element). The vertical column of data could really be three distinct functions, each taking a single parameter.

Ah. This is getting neat: depending on the desired outcome, we can combine data and code in a different order.

## The Matrix Transpose

The matrix transpose swaps rows and columns. Here's what it means in practice.

If `x`

was a column vector with 3 entries (`[3; 4; 5]`

), then `x'`

is:

- A function taking 3 arguments (
`[3 4 5]`

) `x'`

can still remain a data vector, but as three separate entries. The transpose "split it up".

Similarly, if `f = [3 4 5]`

is our row vector, then `f'`

can mean:

- A single data vector, in a vertical column.
`f'`

is separated into three functions (each taking a single input).

Let's use this in practice.

When we see `x' * x`

we mean: `x'`

(as a single function) is working on `x`

(a single vector). The result is the ** dot product** (read more). In other words, we've applied the data to itself.

When we see `x * x'`

we mean `x`

(as a set of functions) is working on `x'`

(a set of individual data points). The result is a grid where we've applied each function to each data point. Here, we've mixed the data with itself in every possible permutation.

I think of `xx`

as `x(x)`

. It's the "function x" working on the "vector x". (This helps compute the covariance matrix, a measure of self-similarity in the data.)

## Putting The Intuition To Use

Phew! How does this help us? When we see an equation like this (from the Machine Learning class):

I now have an instant feel of what's happening. In the first equation, we're treating $\theta$ (which is normally a set of data parameters) as a function, and passing in $x$ as an argument. This should give us a single value.

More complex derivations like this:

can be worked through. In some cases it gets tricky because we store the data as rows (not columns) in the matrix, but now I have much better tools to follow along. You can start estimating when you'll get a single value, or when you'll get a "permutation grid" as a result.

Geometric scaling and linear composition have their place, but here I want to think about information. "The information in x is becoming a function, and we're passing itself the a parameter."

Long story short, don't get locked into a single intuition. Multiplication evolved from repeated addition, to scaling (decimals), to rotations (imaginary numbers), to "applying" one number to another (integrals), and so on. Why not the same for matrix multiplication?

Happy math.

## Appendix: What about the other combinations?

You may be curious why we can't use the other combinations, like `x x`

or `x' x'`

. Simply put, the parameters don't line up: we'd have functions expecting 3 inputs only being passed a single parameter, or functions expecting single inputs getting passed 3.

## Appendix: Javascript Interpretation

The dot product `x' * x`

could be seen as the following javascript command:

`(function(a,b,c){ return 3*a + 4*b + 5*c; })(3,4,5)`

We define a function of 3 arguments and pass it the 3 parameters. This returns 50 (the dot product).

The math notation is super-compact, so we can simply write (in Octave/Matlab):

```
octave:2> [3 4 5] * [3 4 5]'
ans = 50
```

Remember that `[3 4 5]`

is the function and `[3; 4; 5]`

or `[3 4 5]'`

is how we'd write the data vector.

## Appendix: ADEPT Method

This article came about from a TODO in my class notes:

I wanted to explain to myself — in plain English — why we wanted `x' x`

and not the reverse. Now, in plain English: We're treating the information as a function, and passing the same info as the parameter.

## Other Posts In This Series

- A Visual, Intuitive Guide to Imaginary Numbers
- Intuitive Arithmetic With Complex Numbers
- Understanding Why Complex Multiplication Works
- Intuitive Guide to Angles, Degrees and Radians
- Intuitive Understanding Of Euler's Formula
- An Interactive Guide To The Fourier Transform
- Intuitive Understanding of Sine Waves
- An Intuitive Guide to Linear Algebra
- A Programmer's Intuition for Matrix Multiplication
- Imaginary Multiplication vs. Imaginary Exponents

BobSeptember 14, 2016 at 6:22 pmKalid,

Thanks, nice insight. I’m taking linear this semester. I’ve dabbled in the subject before. Always look forward to your emails.

kalidSeptember 15, 2016 at 8:51 amThanks Bob, glad you’re enjoying them.

Frantzelas G.September 15, 2016 at 9:52 amKalid, thank you. This is as always a great insight. Viewing matrix multiplication as operation and data, naturally your mind flows to why matrix multiplication is not commutative.

kalidSeptember 15, 2016 at 3:01 pmThanks Frantzelas! Exactly, it’s great having an intuition on why you can’t just swap the order and expect the same result.

Steven KrouseSeptember 15, 2016 at 2:55 pmAs a LISP programmer, I really jive with the code-is-data intuition for matrices in a way I never have before.

This might be helpful for explaining code-is-data to non-programmers: https://en.wikipedia.org/wiki/Homoiconicity

kalidSeptember 15, 2016 at 3:02 pmThanks Steven :). Only dabbled in LISP but it’s nice how the quote operator (‘) is similar to the transpose in Matlab/Octave (‘) — you are changing from the operation-viewpoint to the data-viewpoint.

BrianSeptember 15, 2016 at 5:59 pmThank you for this insight Kalid. I am trying to translate some very complex mathematical code from Matlab to Python and this is exactly what the underlying principle was. I am truly grateful!

kalidSeptember 16, 2016 at 9:57 amThanks Brian! Awesome to hear it came in handy.

DennisSeptember 16, 2016 at 9:54 amI’ve never done matrix multiplication, but need to learn. I have no idea what any of this means…. do you have another article (or 3) that covers the preliminary steps?

kalidSeptember 16, 2016 at 9:56 amHi Dennis, yep, check out this one:

https://betterexplained.com/articles/linear-algebra-guide/

Brian SlesinskyOctober 1, 2016 at 11:23 pmThis part is hard to parse. I had to read it several times before I finally understood:

> [3; 4; 5] means x = (3, 4, 5). x is a vector of data (I’m using ; to separate each row).

I interpreted this as the following statement:

x = (3, 4, 5). x

That is, “.x” is accessing the “x” field.

Actually, the period is the end of of a sentence, followed by “x” which is the first word of the next sentence. Confusing!

kalidOctober 3, 2016 at 11:23 amWhoops, thanks! Just clarified that part.

DerekDecember 16, 2016 at 5:47 pmBtw, if you are looking to expand on a programmers viewpoint for Matrix Multiplication, I’d highly recommend “Coding the Matrix” by Klein.

The physical book sells for a ~$30 in the US, and $10 on the Kindle (which I think is a global price?). These are extremely reasonable prices for textbooks. The book is explicitly about tying Linear Algebra computer science applications. It’s a very good read.

NIHAL V SJune 23, 2017 at 4:50 amCan you write up a physical intuition for the usage of matrix?

Kishore V.September 4, 2017 at 11:39 amThis post is useful: https://kishorepv.github.io/Matrix-Multiplication/

MikeDecember 5, 2017 at 8:20 pmVery interesting Kishore, much appreciated.

Arunpradhap NatarajanOctober 27, 2017 at 2:12 pmwhy matrix multiplication takes row and multiplies with column? why it should not be row and row multiplication ? like for example let [20 10 5] be the cost of [rice wheat sugar] if I want to buy a 4 kg of rice 2 kg of wheat 3 kg of sugar

the [20 10 5]*[4 2 3]=20*4+10*2+5*3=80+20+15=115

why the multiplier should be column ?