## DEV Community is a community of 700,827 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Matrices, What Are They Good For?

Summer Rizzo
Freshly baked programmer and general blob. Software Engineer slippin n slidin in NYC.

The Matrix (1999) is a science fiction film that explores the idea of reality as we know it being a simulated experience.

Okay, now that that's out of the way, we can move onto the cream of the milk. What are matrices, why do we use them, and what do we use them for?

Many of our first introductions to matrices in programming have been related to algorithmic exercises, usually to test our comprehension of traversing nested data structures. And it may have stopped there. As such, sometimes when our only interaction to concepts is in an academic context, it can feel futile and pointlessly difficult to understand. So, I wanted to further explore their purpose, especially practically.

Most simply, a matrix is a way to represent data. The data is typically numerical in some way or another - it could be a set of numbers, symbols, or expressions. It's represented as a grid, or a 2D array, like this:

``````[ [ -1, -1, -1 ],
[ -1,  9, -1 ],
[ -1, -1, -1 ] ]
``````

So, as you can see here, we have an array consisting of three arrays, each one being a row in the matrix, and each one consisting of three elements. A 3x3 grid, with 9 elements total. For good measure, let's go over some basics. If we wanted to access the top right element, we would access the first row (`row = 0`, using zero-indexing) and then the third and last element ( `column = 2`), or `matrix[0][2]` . If we wanted to access the center element - the `9` - we would access the second element in the second row, or `matrix[1][1]`.

Essentially, a collection of some rows and some columns. What's so special about that? Well, as goes for any data structure, its power comes from how it's leveraged. Barring special types, matrices have fixed dimensions - so, while the data inside it may change, a matrix of size 3x3 like the one above will remain a 3x3 matrix. The containing data is also related in some way. Sure, if you are implementing the matrix yourself, you could remove a row or a column or throw in some random data. But it would kind of make it obsolete, right? Abstract as it may be, matrices are what they are because they are meant to serve a specific purpose - that's kind of the beauty of data structures in general.

One of the benefits of the matrix structure is its ability to apply linear transformations. I'm going to use the definition of a linear transformation found here:

"A linear transformation is a function from one vector space
to another that respects the underlying (linear) structure of each vector space"

There's a lot of math that surrounds this - specifically linear algebra - and I'll provide a few links for further reading below. That is, however, outside of the scope of this post. To keep it brief, operations that can be simplified to linear mapping are a perfect application for working with matrices. Consider this simple example:

Let's scale that idea up with some complexity and practicality. We have a digital image file, represented as pixels. Those pixels are represented in a grid - a matrix. We want all the pixels in the image to change by some amount - maybe we want the image to be brighter. Remember that pixels are often represented as the combination of red, blue, and green channels, each ranging form 0-255 in value. In the digital space, color is additive - so, the higher the value, the more saturated and brighter a color becomes. Brightness filters can be more complex, but for our sake, let's just say we want to increase each `rgb` value by `30`. See where we're going, here? Each value needs the matrix needs to change by a fixed amount - `30`. A linear mapping - a case of a matrix addition!

Matrices have a huge standing in graphics processing for cases like the above. It can be used for other operations, like image rotation, color filters, warping, scaling, convolution* and so on. They are not confined to 2D images - they are made just as useful in 3D graphics rendering as well. Though graphics are a major application of matrices, it doesn't stop there - they are, after all, a data structure, and as such can be useful in general transformation, analysis, and representation of data.

At the heart of it, the take-away is this: because of their structure, data that is represented in matrices can make otherwise complex operations very efficient. As Larry Hardesty notes in an MIT News article on matrix application:

"...that points to one of the reasons that matrices are so common in computer science: They allow computers to, in effect, do a lot of the computational heavy lifting in advance. Creating a matrix that yields useful computational results may be difficult, but performing matrix multiplication generally isn’t."

And that's the whipped cream and sprinkles. We humans do just as much Big Brain work as necessary and pass it onto the computer to do the real Beefy Barbarian work for us.

Of course, it's all about using the right tool for the job. You might not need to work with matrices very often, or you might make heavy use of them. When it's the latter, you'll sure be glad they exist.

With <3, happy coding!

## Discussion (7)

Ben Halpern

Well written!

☮️✝️☪️🕉☸️✡️☯️ • Edited

LERPS. Matrices are common place in graphics programming and analytical continuance of extrapolation.

en.wikipedia.org/wiki/Linear_inter...

Fun stuff i know. I enjoyed reading your article. Useful information. I do agree that a lot of development work does not require higher mathematics or calculus to achieve the desire result.

You also need them for handling complex numbers like Rate of Change of Rate of CHange or fun stuff like Maxwell or Gauss equations which are commonly used in string and byte transforms.

Thank you for sharing and good luck; happy coding.

Galuh Utama • Edited

Great article!

A few years ago I worked for a company and worked extensively with (3D) computer graphics. I think that was the time I used my math skills I learned from the university the most. Matrices are used for camera views, positioning, object transformation, etc in computer graphics. Efficient matrix calculation is extremely important in 3D games if we want to achieve 60 frames per second or more. We even offloaded the matrix calculation to the GPU to make the process faster. Good times.

Nowadays, these calculations are hidden away under graphics/game libraries so there’s not many engineers who understand the calculations behind it. While it’s good, I find it pretty unfortunate.

Summer Rizzo

I likewise have a love/hate relationship with abstraction. It's powerful, and as face-paced as the development world demands, pretty necessary as well. I get a little sad working with libraries sometimes - it's like, I wanna be the one to write the cool code! Let me do it!

Alvaro Saburido

There is a lot of applications, in Telecommunications, all the signals and electronic circuits can be calculated/transform with Matrixes, in fact, Matlab uses iterative analysis and design processes with a programming language that expresses matrix and array mathematics directly.

Kenneth Johnson

Thank you. You make important points. Matrices are super useful. The OpenGL framework, is a good example of how they are used for 3d graphics.

John Kazer

I have been considering the relationship between matrices and graph databases. If you have a lot of numerical information in vertices and edges does it make sense to calculate their relationships with matrices?