Home » Featured, Maths, Matrix

How to Read a 3×3 Matrix

17 January 2010 33,174 views 20 Comments

Matrices are simple but like anything they take a bit of getting used to. I decided to write this article to help newcomers understand a bit more about them.

It is a summary of an explanation I’ve frequently used to develop intuition surrounding matrices. I’ll be assuming very little prior knowledge apart from that you are familiar with 3D applications (it’s not strictly necessary, but the analogies will make more sense). By the end, I’m hoping that you can look at a matrix and have a fair idea what it represents. It’s not an approach I’ve often seen, but try following it and see if it makes sense to you.

Introduction

A 3×3 matrix is used by a 3D application to describe the orientation and a scale of an object in space. (Note that if you want to include the object’s position, then you need a 4×4 matrix, but we don’t want to go there yet.)

It is made up of 9 numbers arranged like this:

ReadAMatrix_Identity

This particular 3×3 matrix is known as the “identity” matrix. We have zero rotation and a scale of 1. It doesn’t do anything to a point in space, no rotation or scale, so it’s a good place to start.

Okay, so this is the important bit. Instead of looking at this matrix as a bunch of numbers, let’s think about it as representing the axis icon in the corner of your XSI/Houdini/Maya viewport.

AxisIcon

The first line of the matrix represents the X axis, the second line the Y axis, and the final line the Z axis. This makes more sense when we think of the end of each line of the matrix being a point in space. This means that each axis looks like this.

ReadAMatrix_VecIdentity

So the first line is a point in space that is 1 unit along the X axis, the second line is a point at 1 unit along the Y axis, and similarly for the final line 1 unit along Z.

So now, if we want to figure out what a particular matrix looks like, just imagine doing the transform to the axis icon and see what happens.

Scaling

What would a 2x uniform scale do to the icon? It’d move each point to double it’s original position along each axis. Okay, well that’s easy to write:

ReadAMatrix_Scaling

The video clip below shows what happens to the axis icon when we scale it uniformly by a factor of 2. I’ve annotated the end of each axis with it’s position. In the top left you can see the three point positions in order, shown to appear as a 3×3 matrix.


Rotation

Starting with the identity matrix again, what would a 90 degree rotation around the Y axis give us? Well, that depends which way we rotate it. Looking down on the Y axis, an anti-clockwise rotation would move the X axis to point along the negative Z axis, and the Z axis to point along the positive X axis.

So for each point, just visualise where it ends up and write down its position.

ReadAMatrix_RotateY

Yep, it’s that simple. Check out the short clip below to see it in action.


If you happen to know a bit of scripting, you should be able to obtain the matrix of an object in your 3D scene and check for yourself that this really works.

Reading the Matrix

So why don’t we try to take a matrix and work backwards to see if we can figure out what it represents.

ReadAMatrix_RotateX

Compare the first line to the first line of the identity matrix at the top of this article. It’s exactly the same, so we know the X axis is unchanged.

Looking at the second row, the Y axis is reversed and now pointing down. The third row shows that the Z axis is reversed too. How would you do this in your 3D software if you were going to use the transform tools?

Actually, there are two ways. You could either negatively scale the Y and Z axes with the scale tool, as shown in the following clip:


Or you could rotate it around the X axis by 180 degrees, shown below:


So are we able to say which one? No, the matrix only tells us about the destination, not the journey.

Technical Detail

A word of warning, sometimes you’ll see matrices written in a different way. In this article, I have used the “row major” form where you can read the X, Y, and Z horizontally. Sometimes you’ll also see them written in “column major” format where you can read the axis vectors vertically instead. This can often lead to confusion when using the above technique.

If you research this further, it is usually stated (Wikipedia) that this difference in format only refers to the internal storage order of the numbers in computer memory, but I have come across situations where it has been written on the page in different ways too.

Using What You Know

Here’s just one quick example of how you can use this knowledge:

Q. Imagine we have a scene with a boat bobbing around on a rough sea. How do we find out how upright this boat is? Another way of thinking about this is how aligned is the boat’s y-axis is with the world’s y-axis?

A. If we have the boat’s 3×3 matrix, we know from the discussion above which row represents the y-axis of the boat (the middle row). With a bit of thought, we can figure out that if the boat is fully upright, this middle row of the matrix should read [ 0 1 0 ]. Taking this further, we know that all we care about is the height of this point (who cares if it’s offset to the side in x or z right?), so really we’re only interested in the y coordinate of this point, i.e. the middle value.

This value gives us the perfect measure of how upright the boat is. A value of 1 indicates it is perfectly upright, and -1 means it is completely upside-down.

ReadAMatrix_Upright

Going Further

Now that you’re hopefully familiar with the basic anatomy of a 3×3 matrix, the next step is to have a look at how it can be used to transform geometry. This isn’t a big leap; you’ve already seen visually how it works for the three points that lie on the axes.

After that, you’ll be more than ready to look at 4×4 matrices which incorporate translation as well, but this is all material for future articles.

1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.86 out of 5)
Loading ... Loading ...

20 Comments »

  • Satish 'iluvblender' Goda said:

    Very nicely written. Thank you for sharing your knowledge.

    Cheers from India,
    Satish.

  • MarkWells said:

    Andy, I like the simplicity and the videos to show what your talking about. Thank you for posting this. Are you going to use this blog as mathematical knowledge or show how you can manipulate matrices in Houdini? I look forward to seeing more entries.

  • AndyN (author) said:

    Thanks for the feedback guys.

    @MarkWells

    Actually, I’ve not had that much need to mess around with matrices in Houdini, mainly because I find they’re not particularly well exposed as a mathematical type (VOPs is okay, but expressions… yuck!). I tend to use them much more in XSI’s ICE which directly supports 3×3 and 4×4 matrix types.

    For future articles, I’ll probably cover transforming vectors with 3×3 matrices, before moving on to 4×4 matrices and the subtleties of 4 element vectors. I’ll be trying to keep the discussion as package agnostic as I can so that it’s useful to more people.

    I’d imagine that anything specific to a particular package like Houdini, I’d write as a standalone article. Let me know if there’s anything in particular you’d be interested in seeing covered.

  • nathan said:

    Great post! Interesting read even for a long time graphics guy. Thanks for putting this together.

  • Abhijeeth said:

    This was awesome !!!
    Thank you so much for putting this effort.
    You have no idea how long i have been pestering people i know to help me understand how matrices affect translations and rotaions of objects in 3D space. Very eagerly looking forward to your future posts.
    Thanx a lot again ! :)

  • AndyN (author) said:

    @nathan
    No problem! Thanks for leaving a comment.

    @Abhijeeth

    Thanks. Great to know that you found it so useful.

  • Christopher said:

    You are a EXCELLENT TEACHER. When I was reading the tutorial, I was soaking it in like a sponge. Could we see an example of a simple thing using 3×3 Matrix in ICE ? Your Video tutorials were excellent to see visually what is going on.

  • AndyN (author) said:

    Thanks Christopher. Yep, this article is quite abstract and just examines the matrix itself, so it doesn’t necessarily lend itself to practical examples.

    You’re right, ICE is a great way of demonstrating the maths very clearly, but I’ll probably put examples in separate posts to avoid directing maths articles towards a specific 3D application.

  • Christopher said:

    Please, I hope so. I understand some people viewing this will grasp it quicker then I, but some examples would help to understand it a little deeper, hopefully you have the time to do such things.

    More of these tutorials is beneficial as you clearly have the Teaching skill. Excellent. Thank You.

  • Mario said:

    Nice and clear for a newbie like me, as you told me. Now I need more… hehe

    Looking forward to see some more practical examples and to read about 4×4 matrices from you.

    Golden material. Thanks a lot for sharing.

  • AndyN (author) said:

    Heh! That’s good to hear. :-)

  • Ana Miranda said:

    You must have been my teacher at University. I would have 20 (A) in Math… Damn!!!! It’s so simple and I never understood what was about. Next year I’ll finish it fou sure. “Golden material” as Mario said. And it makes me feel that no one ever explained it to me in a simple way. Thank you for sharing.

  • AndyN (author) said:

    Just a quick heads up to anyone subscribed to this thread by email. There’s a new article following on from this one. You can find it here:
    http://www.andynicholas.com/?p=911

    Cheers,
    Andy

  • AndyN (author) said:

    (and a belated thank you for your kind comment Ana ! )

  • Avik said:

    great post. made it sound like a piece of cake!
    I’d like to know more about operational mathematics involved with SideFx Houdini. Can you guide me to some resources on the internet or books. I’m finding it difficult to compute equations for normals and other attributes which require a procedural approach. Thanks in advance!

  • AndyN (author) said:

    Thanks Avik.

    My advice would be to not focus on the specifics of any particular package yet. The problem is that each 3D app usually has it’s own way of handling the maths. By looking at the app first, you’re putting the cart before the horse.

    Try learning the theory first. It might seem like more work, but it provides a good consistent foundation to start from.

    To that end, I can totally recommend having a look at the videos here:

    http://www.khanacademy.org/

    If you need Houdini specifics, then http://forums.odforce.net/ is the place to go.

    Often, it can be easier in Houdini to do the maths by writing code with the Inline VOP. You can find more information on that here: http://www.andynicholas.com/?p=19

    Cheers,
    Andy

  • robert said:

    @andy:
    khanacademy.org – thank you so much for this link!

    robert

  • Jangwhoan Choi said:

    @andy:
    Thank you for the clear explain! Now I am the only ONE (matrix)!

  • Mike Rhone said:

    Thanks for this, Andy! You have explained something that has eluded me for a while into an exceptionally simple concept. Thanks for this!

    Mike R

  • AndyN (author) said:

    Not at all Mike. Thanks for letting me know. Glad you found it useful.

    Cheers,
    Andy

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

Security Code: