# How to use matrices in C++

**URL:** <https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927>\
**Category:** C++\
**Tags:** matrix, unreal-engine, UE4, question, CPP\
**Created:** [June 8, 2018, 5:12am UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927 "2018-06-08T05:12:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![B.B.Fresh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@B.B.Fresh](https://forums.unrealengine.com/u/B.B.Fresh)\
**Post date:** [June 8, 2018, 5:12am UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927/1 "2018-06-08T05:12:32Z")

</div>

Please, is there a way to easily define matrices and perform matrix of any size operations in C++? I can’t find any way to do so: seems like FMatrix is only 4x4, seems like TMatrix does not have any operator for operations… While searching for a solution I found class named Matrix33 in “ThirdParty/openexr/Deploy/include/ImathMatrix.h” that seems to fit the purpose (at least for 3x3 matrices) but I can’t find a way to use it in my code  
I’m desperate… Please help… :’(

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a9f83bc6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a9f83bc6](https://forums.unrealengine.com/u/anonymous_user_a9f83bc6)\
**Post date:** [June 8, 2018, 7:38am UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927/2 "2018-06-08T07:38:04Z")

</div>

Hello!

No easy way, though matrix seems very intuitive and simple for a client they are devious,crooked for a programmer.

They hide insidious issues… Sorry,didn’t meant to scare you, but pay attention.  
The can lead to memory/CPU leak, wrong behaviour…

That said, you can overload operators to add matrix operations.

Or you can make your own matrix type… I suggest to make a one dimensional array and add logic to mimic matrix behaviour… For example… If you want a 5x5 make an array of 25 and use X+Yx5 formula to access elements

---

<div class="post-metadata">

**Author:** ![B.B.Fresh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@B.B.Fresh](https://forums.unrealengine.com/u/B.B.Fresh)\
**Post date:** [June 9, 2018, 7:37am UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927/3 "2018-06-09T07:37:41Z")

</div>

Hi,  
I didn’t know matrices were such an issue, I guess I have no choice then ^^  
Thank you for your answer and your suggestion 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_67d829db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_67d829db](https://forums.unrealengine.com/u/anonymous_user_67d829db)\
**Post date:** [June 25, 2018, 11:45pm UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927/4 "2018-06-25T23:45:27Z")

</div>

What are you trying to accomplish? What matrix operations do you need? There’s no need to be scared of matrices.

---

<div class="post-metadata">

**Author:** ![B.B.Fresh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@B.B.Fresh](https://forums.unrealengine.com/u/B.B.Fresh)\
**Post date:** [June 26, 2018, 2:58am UTC](https://forums.unrealengine.com/t/how-to-use-matrices-in-c/426927/5 "2018-06-26T02:58:05Z")

</div>

Hi,  
I needed matrices for basic operations like matrice multiplication, transposition and inversion. I tried to build matrices with arrays as suggested above but I wasn’t very comfortable with arrays (still a beginner) so I ended up using the Eigen library alongside with Unreal Engine. I don’t know if it will cause any issue but it worked fine so far.  
Thank you for your answer ^^
