Asked by:
The syntax of creating the following matrix in c#
Question

Hi guys, I'm new to C# programming and I'd love to learn how can I convert this type of C++ matrix code in the form of C# language. The following the is sample code I have.
Mat eulerAnglesToRotationMatrix(Vec3f &theta) { // Calculate rotation about x axis Mat R_x = (Mat_<double>(3,3) << 1, 0, 0, 0, cos(theta[0]), sin(theta[0]), 0, sin(theta[0]), cos(theta[0]) ); // Calculate rotation about y axis Mat R_y = (Mat_<double>(3,3) << cos(theta[1]), 0, sin(theta[1]), 0, 1, 0, sin(theta[1]), 0, cos(theta[1]) ); // Calculate rotation about z axis Mat R_z = (Mat_<double>(3,3) << cos(theta[2]), sin(theta[2]), 0, sin(theta[2]), cos(theta[2]), 0, 0, 0, 1); // Combined rotation matrix Mat R = R_z * R_y * R_x; return R; }
This code is using EmguCV matrix (if I'm not mistaken) and most of the online solutions guide me on how to use an array to create a matrix. Instead, I'd like to know how this can be done in C#. Thanks in advance.
 Edited by StevenWong1995 Monday, April 9, 2018 10:56 AM
All replies

.NET doesn't really have matrix support for math. You could use a multidimensional array but it would be inefficient. The System.Drawing namespace has a matrix type but it is really for graphics programming.
I'd recommend that you take a look at the System.Numerics namespace/assembly instead. It has some constructs for working with mathematical matrice. If that doesn't fit your needs then you'll need to use a thirdparty library.
Michael Taylor http://www.michaeltaylorp3.net
 Proposed as answer by Fei HuModerator Thursday, April 19, 2018 2:18 AM

Hi StevenWong1995,
>>This code is using EmguCV matrix (if I'm not mistaken) and most of the online solutions guide me on how to use an array to create a matrix. Instead, I'd like to know how this can be done in C#. Thanks in advance.
Is EmguCV a thirdparty library like below, if it is, it support C#.
http://www.emgu.com/wiki/index.php/Main_Page
And the following provide related sample with c# for your reference.
http://www.emgu.com/wiki/index.php/Code_Gallery
if you have any issue about emgucv, you could post your issue on the following link for suitable support.
https://github.com/emgucv/emgucv
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.