MATLAB essential/General information + arrays and matrices: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 22: Line 22:
</div>
</div>


{{BookCat}}<syntaxhighlight lang="matlab">
{{BookCat}}
Open your MATLAB or go to this [http://www.tutorialspoint.com/matlab/try_matlab.php website] if you do not have access to MATLAB.
f=@(x) 2*x
</syntaxhighlight>


==What is MATLAB==
==What is MATLAB==


[[:w:MATLAB|MATLAB]] is short 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 no one has ever mastered MATLAB yet, according to my professors. MATLAB can be used to preform a variety of tasks, such as image processing, solving mathematical equations, video processing, drawing 2D and 3D shaped, and robots programming.
[[:w:MATLAB|'''MATLAB''']] is short for '''MAT'''rix '''LAB'''oratory. MATLAB consists of one giant core of data, that has various toolboxes attached to it. There are so much functions MATLAB can preform that no one has ever mastered MATLAB yet, according to my professors. MATLAB can be used to preform a variety of tasks, such as image processing, solving mathematical equations, video processing, drawing 2D and 3D shaped, and robots programming.


==How will I use knowledge obtained from this course ==
==How will I use knowledge obtained from this course ==

Revision as of 04:33, 22 October 2015

Type classification: this resource is a course.

Open your MATLAB or go to this website if you do not have access to MATLAB.

What is MATLAB

MATLAB is short 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 no one has ever mastered MATLAB yet, according to my professors. MATLAB can be used to preform a variety of tasks, such as image processing, solving mathematical equations, video processing, drawing 2D and 3D shaped, and robots programming.

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; however, you must learn to walk before we can run. This course basically will teach how to walk. Basic stuff like the function of each window, how to difine a variable, and other things that you have to know before using any toolbox in MATLAB will be taught here.

Let's get started

What are these window for

Typical MATLAB window


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

1_ Command window (bottom of the picture): that is where you enter commands which you 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 know values in this window. For example, if you enter

Y=5
Z=6
X=Y+Z

the answer will be 11


2_ Editor (upper right side of the picture): There are several differences between Editor and 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. When you use the editor you can save your work in a file called the M-file for later.
When you run your code after you type it in the editor, matlab will ask you to save a file called the M-file on your computer before you can run it. Your file will have a suffix (.m). When you run your code in the editor , you will find the output in the command window.

3_Workspace: In the work space you can see 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 handy when dealing with arrays and vectors. Refer to the image to the left.

Workspace that contains all the variables (left side)

Important information to keep in mind

  • To run your code, the user have to 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. For example, if we want to create a variable x, we write:
syms x;
  • 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

and press *Enter*. MATLAB will give ALL the information available about this function.

  • 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 outcome of commands. Mostly used when the user wants to enter several commands or a big chunk of code that he/she already know the outcome of. The purpose 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. Must be inserted before comment
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 bu clicking on the cell and pressing ( ctrl+Enter)
At sign @ Used to define a function. Example: f=@(y) sin(2.*y). More information about this topic in next lecture.

For more information about these functions, and to know more important symbols click here

Arrays and matrix

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

an example of a matrix

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:


 

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


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= B=
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 =
- Subtract two arrays or matrices element-wise A-B =
. * Element wise multiplication: multiply each element from matrix A with an element that has the same location in matrix B Exception: when the number of rows of one array equals number of columns of the second array A. *B =
* Matrix multiplication which Not' element wise. ONLY when number of rows of one array is equal to number of columns of second 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 =
. ^ Element wise power B. ^3 =
: creates an array from the value on its left, to the value on its right C=[ 4: 10] =

What if the two matrices do not have the same dimensions

How matrix multiplication is done

Let's assume wen have the following two arrays.
V= = [1,2,3 ; 2,3,4]
X= = [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 raw of matrix X. After that, it adds the three number together. This procedure is repeated by MATLAB again of matrix V.

V*X= = = [20 ; 29]

How matrix multiplication is done


I have inserted some visual aids to help you understand the idea. One of the visual aids is a gif file, so you have to press on it to open it. If you want to learn more about matrices multiplication you can visit this page.

If you want to learn more about the operation that can be done to arrays, you can take a look at this [http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html page. This page is very useful, however it does not offer many examples, you can refer to my previous examples to better understand the operation.


How to break one long matrix into 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 two halves or if you want to form a smaller array from this big array, it would be very tedious and time-consuming to this manually. There is a special function that when used 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

Indexing Matrices

Video games and MATLAB

There is a very famous rule in soccer called the offside rule. When 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 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 the computer, so they used a big MATLAB matrix. This matrix is similar to the one used in the game, but it is much smaller.

X=
This matrix represent the position of the players on the field. The zeros represent empty spots, the ones represent players of team A, the two represent player from team A with the ball, the threes represent players from team B, and the four represent player from team B with the ball. the first raw represent the goalkeeper of team A, the last raw represent the goalkeeper of team B. Looking at the matrix above, you can see that the player from team A with the ball (number 2) is in the third raw, if this player passes the ball to his teammate (3) in second raw, this will be offside because he is close to the team A goal than any other team A player. When the output of this matrix is "true" then the game will stop and the ball will be given to the other team. this was taken from Mathworks.com

Do you have a question about the material covered, Click here to send me an email.

External Links

You can use these links to learn more about this lecture: