# Best method of reading 4x4 matrices of various orientations?

This topic is 686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When I say "reading", I meant to say, "reading the line of code and picturing the numbers in relations to what each member of the matrix is referring to." This is not referred to as "a compiler reading the lines of code.".

I have come across an interesting observation where there are many orientations of a written 4x4 matrix.

Some matrices used in some libraries are shown as:

[0] [4] [8]  [12]
[1] [5] [9]  [13]
[2] [6] [10] [14]
[3] [7] [11] [15]

And some matrices are shown as:

[0]  [1]  [2]   [3]
[4]  [5]  [6]   [7]
[8]  [9]  [10]  [11]
[12] [13] [14]  [15]

For row-major and column-major matrices, they would also have orientations laid out as either one of these:

[1] [0] [0] [0]
[0] [1] [0] [0]
[0] [0] [1] [0]
[0] [0] [0] [1]

[0] [0] [0] [1]
[0] [0] [1] [0]
[0] [1] [0] [0]
[1] [0] [0] [0]

Depending on Big Endian or Little Endian order. Do note that I am unsure of this, as to why the diagonals of the matrix is forward/backward slanting, depending on how the graphics chipset on various devices reads the data structure. I'm assuming it to be Big / Little Endian based on the reading order of the memory.

Combining these findings, I can see there are 4 types of matrix orientations commonly used in various libraries for various devices/applications. I want to know if my proposed method below is the best method of reading these various matrix orientations, and holds the concept:

------------------

For instance, given the following matrix:

[0] [4] [8]  [12]
[1] [5] [9]  [13]
[2] [6] [10] [14]
[3] [7] [11] [15]

We can assume this is the case in an OpenGL environment:

RT = right
UP = up
BK = back
POS = position/translation
US = uniform scale

[RT.x] [UP.x] [BK.x] [POS.x]
[RT.y] [UP.y] [BK.y] [POS.y]
[RT.z] [UP.z] [BK.z] [POS.Z]
[    ] [    ] [    ] [US   ]

To humanly read a matrix, instead of looking at the matrix aligned in a 4x4 grid, we should look at each individual elements used in the matrix, as if the matrix is an array.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]


This means, by reordering our matrix to the following:

[0]  [1]  [2]   [3]
[4]  [5]  [6]   [7]
[8]  [9]  [10]  [11]
[12] [13] [14]  [15]

And knowing the relations of each member element of the matrix as:

RT = right
UP = up
BK = back
POS = position/translation
US = uniform scale

[RT.x]  [RT.y]  [RT.z]  [    ]
[UP.x]  [UP.y]  [UP.z]  [    ]
[BK.x]  [BK.y]  [BK.z]  [    ]
[POS.x] [POS.y] [POS.Z] [ US ]

We can let a human read all matrix orientations by mapping the indices of the array to the associated properties of the matrix:

[0..2]   = RT
[4..6]   = UP
[8..10]  = BK
[12..14] = POS
[15]     = US

And read the indices in reverse/forward for each sub-array based on Big or Little Endian. This, in turn, can be easier to translate into code for the compiler to read, without worrying about any various 4x4 matrix orientations. Edited by tom_mai78101

##### Share on other sites

An individual element in a matrix does not have a 1:1 ratio with regards to its mathematical representation. It is a combination of arithmetic operations on the elements of the matrices that will give you the final outcome. A 5 in position _22 does not just represent "rotation on x axis", or "scale on Y axis". These two operations are combined into a single number.

Your question is slightly confusing, but I guess I would answer that you have to work in the context of the situation. If you are working in directX or openGl, the matrices will be represented differently, ect... If you mean how to understand them for pencil/paper operations, the same applies.

Edited by ExErvus

##### Share on other sites
The question is asking about human readability, to read the various matrix orientations in a way that the method can be applied to all situations.

So, when the human is reading the line of code and picturing the numbers, the human will project the relations for each member of the matrix by referring it to the mathematical representation of an ideal matrix that the human has a preference over.

In other words, since every developers have their own ways of piecing the matrix data together in their minds, one of which is "considered" better than the other based on preference, I am proposing a shortcut method interpreting the data into a very high-level concept that the human can grasp, and can be "translated" to their respective context given for various libraries.

I have this idea when I was working on a 3D graphics library, and I still have trouble grasping the concept of a different matrix orientation that I'm unfamiliar with my math textbook mathematical representation. Whenever I make a big change to a matrix math function, I would sometimes get told the matrix is inadvertently transposed than it was intended, and that kind of confusion would lead me to frustrating debugging situations. With this method I proposed, I find reading matrices in my head a bit easier to do. I'm very sure there are more better methods than my idea. Edited by tom_mai78101

##### Share on other sites

I'm not sure I am following what you are saying. Row major or column major does not matter as long as you pick one and stick with it.

The interior 3 by 3 matrix inside the 4 by 4 matrix is 3 mutually perpendicular vectors that represent the X, Y, and Z axes. If the matrix is:

1, 0, 0

0, 1, 0

0, 0, 1

The three vectors are pointing down the X, Y, and Z axes respectively. Performing transformations on the matrix will preserve their mutually perpendicular arrangement as long as you don't do anything crazy.

So, they can be used to represent an orientation, or "change" from the world's X, Y, and Z axes. In this case, they are aligned and the same as the world axes. But if you rotate this matrix it will rotate out of alignment and store an orientation different from the world's alignment.

The 4th column/row can be used to store a position or offset so that the 4 by 4 matrix stores an orientation, a scale, and a position in 3D space. See my video for more.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 24
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631734
• Total Posts
3001933
×