# MATLAB essential/General information + arrays and matrices

 Open your MATLAB or go to this website if you do not have access to MATLAB on your PC. If you want to use the editor (script on the left) make sure to save your work using the drop-down menu before running it

Course Main Page:

Course Content:

Lecture 1 : General information + arrays

Lecture 2: MATLAB and mathematical equations

Lecture 3: 2D and 3D plotting

Lecture 4: If statements and loops

## What is MATLAB

MATLAB is stands for MATrix LABoratory. MATLAB consists of one giant core of data, that has various toolboxes attached to it. There are so much functions MATLAB can preform that very few people had been able to master it. MATLAB can be used to preform a variety of tasks, such as image processing, solving mathematical equations, video processing, drawing 2D and 3D shapes, and programming robots.

#### Example of MATLAB application:

These are some graphs and gifs created using MATLAB

## How will I use knowledge obtained from this course

Whether you want to become an engineer, mathematician, or a physicist, you have to have an advanced knowledge in MATLAB. This course will teach you the basics of MATLAB. Basic information like the function of each window, how to define a variable, and other information that you have to know before using any toolbox in MATLAB is taught in this lecture.

## Let's get started

### What are these window for

This is what a normal MATLAB window looks like. As you can see, there are three main sub-windows:

1_ Command window (bottom of the picture): in this window the user can enter commands for MATLAB to execute right away. For example: if you want to find a solution for the equation ( X= 70/10 ), all you have to do is type it into the command window and press enter and MATLAB will give an answer in a split second. You can solve equations with several variables with known values in this window. For example, if you enter

Y=5
Z=6
X=Y+Z


the output will be 11

 You may be interested in: Wikibooks_MATLAB_CommandPrompt and Wikibooks_MATLAB_Variables

2_ Editor (upper right side of the picture): There are several differences between the Editor and the Command window. Both can be used to enter commands; however, the editor is used much more often than the command window for these two reasons.

1_ Unlike the command window, in the editor you can go back and and fix (debug) your code after running it. For that reason, the command window is often used if you want to write only a small and easy piece of code.
2_ Once you close MATLAB, all your code in the command window is gone. On the other hand, when you use the editor, you can save your work in a file called the M-file for later use. When you run the code after typing it in the editor, MATLAB will ask you to save a file called the M-file on your computer. This file will have the suffix (.m). The output of the code will appear in the command window.

 You may be interested in: Wikibooks_MATLAB_M.Files

3_Workspace: In the work space you can see all the variables that you have created so far. By double clicking on the variable name, you can open it in a separate window. This becomes useful when dealing with arrays and vectors. Refer to the image to the left.

 You may be interested in: Wikibooks_MATLAB_Workspace

### Important information to keep in mind

• To run your code, you can either press (F5), or press the run button at the top.
• The user should define a variable before using it. Defining a variable is done by using the key word ( syms ) followed by the variable name. Once you define a variable, it is saved in the workspace, so you do not have to define it again even if you erased your code. For example, if we want to create a variable x, we write:
syms x;

• A string contains a sequence of letters or numbers. To declare a string, write your chosen string name, followed by a single quote, then the letters and/or numbers you want the string to contain, then another single quote. For example:
mystring = ' this is a string 123 '

• If the user wants to learn more about a certain function, he/she can type in the command window the word " help " followed by the function name. For example, if you want to learn more about the function syms, you code:
help syms


• To clear commands window, write: ( clc )
• To clear the Workspace, write (clear) in the command window.

### Most used symbols

Name Symbol Function
semicolon ; Used to suppress the output of commands. Mostly used when the user wants to enter several commands or a big chunk of code that he/she already know the output of. The purpose the semicolon is to keep the command window from getting full of unimportant information
Percent sign % Used to insert a comment to explain the function of a certain piece of code.
Double percent %% Used to create a cell. The code inside this cell could be run separably from the code in the rest of the editor by clicking on the cell and pressing ( ctrl+Enter)

## Arrays and matrix

An array is a data structure that consists of a collection of elements values or variables. matrix is a rectangular array that contains numbers and symbols arranged in rows and columns.

Arrays are extremely used in all programming softwares. creating an array is very easy. First, you need to create a variable, then you need to use square brackets to tell MATLAB that you want to create an array. If you want to create a new Column you need to use a Comma, if you want to create a new row you need to use a semicolon. For example:

A= [ 1,2,3 ; 10,20,30 ]


This will give you the following array:

${\displaystyle {\begin{bmatrix}1&2&3\\10&20&30\end{bmatrix}}}$



This is another example: if you want to create the following matrix

${\displaystyle {\begin{bmatrix}1&9&-13\\20&5&-6\end{bmatrix}}}$


You code:

X=[1,9,-13 ; 20,5,6]


### Operations on arrays and matrices

Let's assume we have matrix A and matrix B
A= ${\displaystyle {\begin{bmatrix}10&20&30\\2&5&10\end{bmatrix}}.}$  B= ${\displaystyle {\begin{bmatrix}1&2&3\\20&50&1\end{bmatrix}}.}$
To write A and B in MATLAB we code:

A=[10,20,30 ; 2, 5, 10]
B=[1,2,3 ; 20,50,1]


Operator Purpose Example
+ Add two arrays or matrices element-wise A+B = ${\displaystyle {\begin{bmatrix}11&22&33\\22&55&11\end{bmatrix}}}$
- Subtract two arrays or matrices element-wise A-B = ${\displaystyle {\begin{bmatrix}9&18&27\\-18&-45&9\end{bmatrix}}}$
. * Element wise multiplication: multiply each element from matrix A with an element that has the same location in matrix B A. *B = ${\displaystyle {\begin{bmatrix}10&40&90\\40&250&10\end{bmatrix}}}$
* Matrix multiplication which Not' element wise. ONLY when number of rows of one array is equal to number of columns of the other array A*B= Inner matrix dimensions must agree
. / divides each element from matrix A with an element that has the same location in matrix B. Both matrices should have the same number of rows and columns A. /B = ${\displaystyle {\begin{bmatrix}10&10&10\\0.1&0.1&10\end{bmatrix}}}$
. ^ Element wise power B. ^3 = ${\displaystyle {\begin{bmatrix}1&8&27\\8000&125000&1\end{bmatrix}}}$
: creates an array from the value on its left, to the value on its right C=[ 4: 10] = ${\displaystyle {\begin{bmatrix}4&5&6&7&8&9&10\end{bmatrix}}}$

This table was inspired from MathWorks

What if the two matrices do not have the same dimensions

Let's assume wen have the following two arrays.
V=${\displaystyle {\begin{bmatrix}1&2&3\\2&3&4\end{bmatrix}}.}$  = [1,2,3 ; 2,3,4]
X=${\displaystyle {\begin{bmatrix}2\\3\\4\end{bmatrix}}.}$  = [2 ; 3 ; 4] In order to preform multiplication between two matrices that do not have the same number of rows and columns, we use this operator (*); however, the number of rows in one of the matrices should equal the number of columns in the other. When using this operator on V and X matrices, MATLAB multiplies the first element in the first column of matrix V with the first element in the first row of matrix X, then it multiplies the first element in the second column of V with the first element in the second row of matrix X, then the first element in the third column of V with the first element in the third row of matrix X. After that, it adds the three number together. This procedure is repeated by MATLAB again for the second element of each column in matrix V.

V*X= ${\displaystyle {\begin{bmatrix}(1*2)+(2*3)+(3*4)\\(2*2)+(3*3)+(4*4)\end{bmatrix}}}$  = ${\displaystyle {\begin{bmatrix}20\\29\end{bmatrix}}}$  = [20 ; 29]

Refer to the two visual aid too the right for better understanding.

 You may be interested in: MathIsFun_MATLAB_Arrays_Multiplication AND: MathWorks_MATLAB_Arrays_Opperation

How to break one long matrix into two small ones?

Assume you have a very long array, such as:

Data= [ 12 , 54 , 99, 203, 2045, 1, 5,4, 90, 45, 34 , 345, 124, 3434 ,345, 2, 2, 5, 4, 7, 8, 0, 1, 100 ,6, 78, 2 ];


If you want break this big array into several smaller arrays, it would be very tedious and time-consuming to this manually. There is a special function that can help you form a smaller array and save time. For example, if you want to form an array that contains the first 20 element of this array you can write in MATLAB:

 New_Array=(Data (1:20 ));


This procedure could be done as many times as you want to create several small arrays

 You may be interested in: Wikibooks_MATLAB_Arrays

## Video games and MATLAB

There is a rule in soccer called the offside rule. To understand this rule, consider this example, if a player from team A gets the ball when he is closer to the goal of team B than any other player from team B, this is considered an offside, and the ball is given to team B. So how is that related to MATLAB ? The programmers developing the soccer video game (FIFA) faced the problem of how translating this rule into a language that could be understood by computers, so they used a big MATLAB matrix similar to the one shown below

X = ${\displaystyle {\begin{bmatrix}0001000\\0003000\\0002100\\0003131\\0310101\\0131313\\0300000\\0030030\\0003000\end{bmatrix}}.}$
This matrix represent the position of the players on the field. The 0 represent empty spots, the 1 represent players from team A, the 2 represent player from team B with the ball, the 3 represent players from team B without the ball, and the 4 represent player from team A with the ball. The first row represents the goalkeeper of team A, the last row represent the goalkeeper of team B. Looking at the matrix above, you can see that the player from team B with the ball (number 2) is in the third row. If this player passes the ball to his teammate (3) in second row, this will be offside because the (3) in second row will turn into (2), and this (2) is closer to the goalkeeper (1) than any other (1) on the field. As a result, the output of this matrix is "true" then the game will stop and the ball will be given to the other team.