In this tutorial I will help you to deal with matrices in LabView. LabView has provided us with an option of MathSript in which you can write you code in mathscript and run it as simple LabView code. If you want to manipulate matrices I will suggest you to use mathscript window as we have used before for basic introduction of mathscript window, because it is way easier than 2D arrays manipulation in block diagram designing. In this tutorial you will leanr hoe to Add subtract, and multiply two matrices in LabView with the help of a mathscript code. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them. See our tutorial on** using mathscripts in labview **

### Matrix Addition subtraction and multiplication in labview

- Lets’ move to the coding portion, and try to work with matrices in mathscript LabView, from the main page after opening LabView click on the Tools button on the top bar, a dropdown menu will appear and from that menu click on Mathscript window as shown in the figure below,

Figure 1: Opening mathscript

- From the window appeared by opening the mathscript window click on the script button next to the variables section as shown in the figure below,

Figure 2: Opening script

- This will create a script in mathscript save it for future use by pressing <Ctrl+S> or by pressing the save button made on the top of the script as shown in the figure below,

Figure 3: Saving script

- In the previous tutorial about the introduction to mathscript I have explained the working of arrays; matrices operate in the same way as an array. We can define or initialize a matrix by separating two columns by a semicolon (;) as shown in the figure below,

Figure 4: Matrix definition

- We can run this script by pressing <Ctrl+R> or by clicking on the green arrow button on the top of the script as shown in the figure below,

Figure 5: Running a script

- The output of this line of code will be displayed in the output window as soon as you press the green arrow as shown in the figure below,

Figure 6: Matrix output

- This implies that separating two arrays by a semicolon (;) will generate a matrix with the array before the semicolon as first row of the matrix and array after the semicolon as second row of the matrix as can be seen from the output window.
- If you don’t want each line of your code to be displayed on the output window, simply place a semicolon at the end of the line, this semicolon will suppress the output of the command to be displayed on the output window.
- By default every mathscript command line has a printing option with it and the semicolon will tell LabView to suppress the printing option of the command as shown in the figure below,

Figure 7: Suppressing the output

- Now in order to do mathematical operation on a matrix we have to define another matrix i.e. a matrix ‘b’. Now if we are interested in adding to matrix a simple plus (+) sign will do the task for us, this is the major benefit of using mathscript in LabView. Placing a ‘+’ sign between two matrices will add the matrices according to matrix rules of addition as shown in the figure below,

Figure 8: Matrix addition

- We can do the matrix subtraction in similar manner as but using a minus (-) sign instead of the plus (+) sign. Placing a ‘-’ sign between two matrices will subtract one matrix from the other matrices according to matrix rules of subtraction as shown in the figure below,

Figure 9: Matrix subtraction

- Coming over to the matrix multiplication part, we all know that matrix multiplication is not as straight forward as simple addition and subtraction case. In order to evaluate matrix multiplication we have to take into account the rules defined to multiply two matrices.
- One of all the major benefits of using mathscript is that we can evaluate matrix multiplication by simply placing an asterisk (*) sign between two matrices. Placing a ‘*’ sign between two matrices will multiply one matrix with the other matrices according to matrix rules of multiplication as shown in the figure below,

Figure 10: Matrix multiplication

- But what if we don’t want to implement matrix multiplication and we are only interested in multiplying corresponding elements of two matrices.
- Mathscript also allows us to do so using a dot (.) in conjunction with the asterisk (*) sign. Placing ‘.*’ signs between two matrices will multiply corresponding elements of one matrix with the other matrices as shown in the figure below,

Figure 11: Element wise multiplication

__Exercise:__

- Do all the tasks we have done in this tutorial using a matrix of sixe 3×3.

**<< Previous tutorial Next tutorial>>**