How to define matrices and load data into them in C# – Unity Implementation

Introduction

In mathematics, a matrix is a structure containing numerical data arranged in rows and columns, one of its main uses is to represent and solve systems of linear equation.

In this article you will see how to represent a matrix and load data to it in C# language in Unity Engine.

About the development environment, script creation and execution

For this example we will use the Unity engine, create a C# Script and assign it to a GameObject in the scene, when pressing the Play button, Unity will automatically execute some functions inside the Script.

Implementation of a matrix in programming

Declaration of data

Matrices have a certain number of rows and columns, so we will need two integers, let’s call them “rows” and “columns”.

To declare the matrix in programming we will use a two-dimensional arrays and we must indicate the data type, usually we are going to work with the set of real numbers, so the data type we are going to use is “float”. However we could define arrays of any data type needed, for example “bool”, “int”, “double”, “string”, etc.

The declaration of the required data would be as follows:

Fig. 1: Data declaration to represent a matrix size “rows” by “columns”.

To declare a two-dimensional array, brackets are used and a comma is placed for each extra dimension that is needed, in this case as we need two dimensions it looks like this: “[ , ]”.

Initialization of the matrix data

In the previous step we declared the necessary variables, now it is time to initialize those variables, that is to say to give them concrete values or to create data structures and assign them. The initialization will be done in the “Start” method of the Script, the Start and Update functions are automatically defined when creating a new Script. By pressing the Play button in Unity, the Start functions of all the scripts present in the scene will be automatically executed, this will happen before the the first frame of the program appears on the screen.

In figure 2 we see an example of the initialization of a 2×2 matrix, in lines 17 and 18 is the initialization of the rows and columns respectively. In line 20 a two-dimensional array is created indicating the total number of elements of each dimension and this array is assigned to the “myMatrix” field that was declared in the previous step.

Fig. 2: Inicialización de los datos necesarios para representar una matriz de 2 filas y 2 columnas.

In C# the first element of the arrays is 0 and the last is “n-1”, where n is the size of the array, so in line 22 for example, the data in position [0,0] refers to the element in the first row and first column.

Conclusion

We have seen how to declare two-dimensional arrays that can be used to represent matrices and subsequently perform operations between them.

In this example the data type used for the array is “float”, which means that each element will have a number with decimal point stored, but you could define arrays of any data type you need, for example “bool”, “int”, “double”, “string”.

To read or modify an element within the matrix we use the name with which it was defined and between brackets we indicate the row and column separated by comma, so for example if we write “myMatrix[1,0]” we are pointing to the element of the matrix that is in the second row, first column.

Exit mobile version
Secured By miniOrange