Creating a One and Two Dimensional Array/Matrix, Performing Arithmetic and Matrix operations

Leave a Comment

MATLAB: The Language of Technical Computing
MATLAB: The Language of Technical Computing



Hello Everyone!
Keeping in line with the previous group of rather diverse posts on MATLAB by one of our old partners, I'd like to continue them further with a more meaningful purpose.
As in the title hence, this would be the first stepping stone in learning MATLAB from the basics :)

If anyone of you would like to recap on their knowledge of MATLAB as a programming language, you are highly recommended to do so from here: An Introduction to MATLAB


As the very first step, you are recommended to install the MATLAB application from the official MathWorks website. For your help, the latest release of MATLAB is R2014b. The download link is given here:

http://in.mathworks.com/downloads/web_downloads

I have the R2014a version. There is absolutely no problem whichever version you have as the commands we are going to run will run on all the versions in just the same manner.

Moving on, double click on the MATLAB icon (which looks like the one shown below) on your desktop to get started.

MATLAB Icon
MATLAB Icon

Opening which you should be treated with a window as shown below

Start Window, MATLAB R2014A
Start Window, MATLAB R2014A
Now as you can see,
  1. Current Folder window shows the current folder you are working from, which is the folder your desktop shortcut for MATLAB points to. In other words, it shows the folder which contains the .exe for MATLAB which you executed via your shortcut from the desktop to open MATLAB.
  2. Workspace shows all the variables generated throughout your current MATLAB session.
  3. Command Window is the main window you should care about. This is the place where you enter your commands which in turn are executed and the results are displayed. And we want the results right? So this is the main window!
Now MATLAB makes working with matrices so simple. Following our session, which starts right now, you'd appreciate that.

Creating a One Dimensional Array (1-D Array)

To create a 1-D Array, you just need to enter the following commands into the command window.

A=[1 2 3 4];
display(A);

Here you create an array with the name A. All the elements are entered inside square brackets with spaces between them. Finally you give out a semi colon which refers to the end of statement.
Now as we want to display the above array, we will use the display function. The display function contains the name of the array we want to display. Hence by giving display(A), we ask the application to display the array named as A.

After you enter the above pair of commands, you should get an output like this

Creating an array
Creating an array

Now talking in terms of matrices, what we just produced can also be seen as a simple Row Matrix. We created a row matrix A, which had the elements 1, 2, 3 and 4.
The natural order of things would also ask us to generate a column matrix now. We can do that too, to do so, we have to enter the following commands.

A=[1; 2; 3; 4];
display(A);

This produces an output as following

Row and Column Matrices in MATLAB
Row and Column Matrices in MATLAB
If you'd see, what we just did was to put a semi colon at the end of each element. This caused each element to be taken in as a single row. You can in this way produce row and column matrices in MATLAB.

Creating a Two Dimensional Array (2-D Array)

As we learn creating a 1-D array, its again a natural procession of things that we move to the 2-D Array. To create a 2-D array or a proper matrix, you need to enter the following commands

A=[1 2 3; 4 5 6; 7 8 9];
display(A);

Just as above, the semicolon after an element marks the end of the row in which that element is contained. Hence placing a semi colon after 3 marks the end of the first row and so on. Note that we do not place a semi colon after the last row because the matrix as it is end when the closing bracket is encountered.
In just the same way we can create any type of Matrices. For your convenience, 3 are generated below.


Creating a 2D Square, 3x2 and 2x3 matrix
Creating a 2D Square, 3x2 and 2x3 matrix

And with that, we have an understanding about how to create matrices in MATLAB. Lets now move on to operate upon them.

Performing Arithmetic Operations - Addition, Subtraction, Multiplication, Division and Exponentiation

In this section we would learn how to operate arithmetically over matrices. Let us learn them one by one. For the sake of simplicity, I'll be using 1-D Matrices wherever possible.

Addition

We can add two matrices using the ( + ) operator in the following manner.

A=[1 2 3 4];
B=[1 2 3 4];
C=A+B;
display(A);
display(B);
display(C);

As we did earlier, we created  1-D matrices A and B, added them and stored the result in the 1-D Matrix C. The above set of commands show the following output.

Performing addition of two matrices
Performing addition of two matrices

Also, take a look at the adjacent Workspace window.

Workspace Window
Workspace Window

As perhaps you can see and as we told you earlier, the Workspace window shows you the list of the variables in the current session. The three variables we have right now are A, B and C. The workspace window shows the names of those variables we are using right now, along with their values and the minimum and maximum values contained inside them.

Similarly, 

Subtraction

is performed using the (-) operator.

Commands:

A=[1 2 3 4];
B=[1 2 3 4];
C=A-B;
display(A);
display(B);
display(C);

The above set of commands generate the following output:

Performing Subtraction of two matrices
Performing Subtraction of two matrices

Multiplication

Now multiplication of matrices is a different case altogether. For example, if we continued to multiply our older two matrices, an error will be generated as shown in the image below.
What we need to understand is matrix multiplication follows a basic rule:

"Consider two matrices A and B. If A is an m x n matrix and B is a n x p matrix, they could be multiplied together to produce an m x n matrix C. Matrix multiplication is possible only if the number of columns n in A is equal to the number of rows n in B.In matrix multiplication, the elements of the rows in the first matrix are multiplied with corresponding columns in the second matrix.Each element in the (i, j)th position, in the resulting matrix C, is the summation of the products of elements in ith row of first matrix with the corresponding element in the jth column of the second matrix.In MATLAB, matrix multiplication is performed by using the * operator."

Hence instead if we multiply our old A matrix with a column matrix B instead of the older row matrix B, will we definitely get a successful product of the two matrices because

A(1x4) * B(4x1) = C(1x1)

Hence the following commands:

A=[1 2 3 4];
B=[1; 2; 3; 4];
C=A*B;
display(A);
display(B);
display(C);

will generate the following output:

Performing multiplication of two matrices
Performing multiplication of two matrices

Division

Division by a scalar. It is implemented using (/) operator.

The Commands used are:

A=[1 2 3 4];
B=A/2;
display(A);
display(B);

The above set of commands generate the following output:

Dividing a matrix with a scalar
Dividing a matrix with a scalar


On the same lines, if we want to divide two matrices as in dividing each element of A with each corresponding element in B and storing the resultant elements in a third matrix C, we can use the operator (./) in the following manner.

Commands used for dividing matrix A with matrix B

A=[1 2 3 4];
B=[1 2 3 4];
C=A./B;
display(A);
display(B);
display(C);

will show you the following output.


Performing division upon two matrices
Performing division upon two matrices


Exponentiation

To perform exponentiation over any given matrix, we use the (.^) operator.
For example, consider the following code:

A=[1 2 3 4];
B=A.^2;
display(B);

will raise every element in the matrix A to the power 2. On the contrary, a element can be raised to every element in a matrix and displayed again as a matrix. The commands for the same would be:

A=[1 2 3 4];
B=2.^A;
display(B);

The outputs obtained for the same are as below:


Performing exponentiation over a matrix
Performing exponentiation over a matrix


So with that I believe we have demonstrated the arithmetic operations we far above talked of. Lets now hence move to learning matrix operations.

Performing Matrix operations - Inverse, Transpose, Rank

Moving on to matrix operations, we would be learning how to find the inverse, transpose and rank of a matrix using MATLAB.

Inverse of a Matrix

The inverse of any square matrix (m x m matrix ) is a matrix which when multiplied with the original matrix produces an identity matrix. An identity matrix is a square matrix which contains all 1s along its diagonal.
One thing is worth noting that non-square matrices do not have inverses. Also not all square matrices have an inverse. A square matrix with an inverse is called an invertible or nonsingular matrix while one without an inverse is called non-invertible or non-singular.

The commands used to find the inverse of a matrix are:

A=[1 20 3; 5 6 7; 8 9 10];
B=inv(A);
display(A);
display(B); 

With the following output:

Computing the inverse of a matrix
Computing the inverse of a matrix

Transpose of a Matrix

To obtain the transpose of a matrix, invert all the rows into columns and all the columns into rows. If the original matrix was called as A, the transpose will be called as AT.

The commands to verify the same are as following:

A=[1 20 3; 5 6 7; 8 9 10];
B=A';
display(A);
display(B);

The following output will be produced hence:

Computing the transpose of a matrix
Computing the transpose of a matrix

Rank of a Matrix

One of the most fundamental characteristics of every matrix is its rank. The rank of a matrix is commonly denoted as rank A. It is the largest collection of linearly independent rows (row rank) and columns (column rank). For every matrix, the column rank is equal to the row rank.

Hence to find the rank of a matrix, the commands that need to be executed are:

A=[1 20 3; 5 6 7; 8 9 10];
B=rank(A);
display(A);
display(B);

The above commands will hence produce the following output.


Computing the rank of a matrix
Computing the rank of a matrix



And hence we complete our first fundamental tutorial on MATLAB 1D and 2D array operations. Hope you enjoyed it. See you next time. Take care :)



0 comments:

Post a Comment

Powered by Blogger.