Learn Basic Matlab in 30 Minutes

rupam's picture
Submitted by rupam on
Matlab window and its components

Ok Lets Hope that you have Matlab Installed, if not you can download an evaluation student version from matworks.com website.

Just double click on the Matlab icon in your desktop. A screen appears as illustrated in the image bellow.

Matlab is a scripting language. The meaning is each of the statement that we use in matlab are commands in themselves. The matlab source code that you write are Batch file where number of commands are executed from top of the file to the bottom.

So we can start learning matlab just by typing the commands in the command window.

 for this tutorial i will follow my steps that I have mentioned in my earlier tutorial, "How to become Efficient Programmer".

1. Understanding Variable

In Matlab variables are of unsigned integer (uint), double (default data type), char ( representing a single character) and cell type( representing a string array). unlike c you do not require to specify any datatype. Just declare a variable as bellow.

A=10;

Now semicolon is another important aspect of Matlab, is command is entered without it, it will execute and display. Otherwise Execute but wond display. So not using a semicolon can be used as a display statement also.

In matlab every thing is an Array. so A=10 will Create a one dimensinal array of size 1x1.

Suppose we need to create a row array, where there is a single row and multiple columns

B=[1 2 3 4]; Right a Space ensure that elements have different column.

If you want to declare a column array, one column and many rows,

C=[1;2;3;4]; Thats right a semicolon within array body will ensure that the next element has next row.

so

D=[1 2 3;4 5 6]

is representation of following structute

D
123
456

 

 

 

Thats Right. That Simple. Now to get the transpose of a matrix, i.e. just alter rows and columns, you need

E=D' ; its a single quotation. Therefore E will be [1 4;2 5; 3 6];

Suppose you want to add one extra row with D as [7 8 9], then use following structure

F=[7 8 9];

G=[D;F]  matrices are combined.

if you want to add one extra column of [11 22 33] with G

H=[11 22 33];

I=[G H] so adding element in rows and adding elements in columns follows same method.

in C you write 2 for loops to print the elemnst of array. Here:

just type the matrix element and enter. it will  be displayed

>>D ( enter) remember no semicolon. That will truncate the display.

INPUT

you can take input from user by

A=input('enter the number:')

A can be any number or matrix or string whatever you want. so in case of matrix , in the prompt you must type as bellow

>>Enter the number:[1 2;3 4] ( enter)

 

2. Accessing Members of an Array of Matrix

 If you have followed the above steps correctly then you have a matrix I as follows

 

12311
45622
78933

Suppose we want to print Everything to understand the indexing..

I(1:1:3,1:1:3)

Wow something pretty new you are seeing

1:1:3 is read as Start from 1: in the steps of 1, upto 3

1:1:3,1:1:3

The comma here specifies next dimension . So element before comma represents index for rows and after comma represents value for columns.

alternatively, when you need to access all

I(:,:)

only a column in index represents "All"

I(1:3,1:3) can also be used. if you use this notation, it means that system will consider default "in the steps of 1"

so if you want to print all columns of 1st and third row?

I(1:2:3,:)

Important point to be remembered here is in matlab arry index starts from 1 and not from 0

Suppose you have some program which generates the value for the array and initially you do not know how many elements will be there in the array. ( Simple example is generating all prime numbers between 1 and 30. Can you tell before counting how many element will be there?)

So the declaration can be

J=[];

yes just opening and closing of a square brace.

when you put the elements you can simpley make use of following notation

J=[J element]; where element is the next value that you want to store in J.

3. Operators

well operators are like c/c++/java or .Net. but remember operations will be matrix operation

so 

K=[ 1 2] ; L=[3 4]

M=k+L; will give M=[4 6]

Multiplication is strictly vector multiplication

so K and L can not be multiplied. Remember the rule of   r,c x c,z  so for multiplication of matrices requires column of the first matrix to be same as second matrix.

so if L were L=[3;4] then

N=M*L; ( remember vice versa is not true)

another type of multiplication is possible here.   Called scaler multiplication.

in that size of the matrices must be same.

so if L was L=[ 3 4]

N=L.*M; or N=M.*L; a dot before the star ensures that the operation is scaler.

division is / operator.

modulo operation is tricky.

Normally we use % for modulo in C and C++ and in any conventional language. here in matlab

% is a comment statement

so for getting modulus you need to use the inbuild function mod

P=mod(13,2); will return 1 ofcourse.

You can add the elements of an array by

Q=sum(N); or any array or matrix.

similarly average of a matrix can be found by

mean(P);

remember if its two dimensional matrix, like I, then you have to use 

m=mean(mean(I));

otherwise it will return row wise mean and you will get 3 values.

remember in matlab there is no suport for ++ and --. so you have to use A=A+1; as increment

also >,>= and <, <= are same.

but != that we use in c and c++ differs here. 

~=  is the not equal to operator. Yes Tilt operator.

!operaot (Not Operator) will inverse 0 with 1 and 1 with 0 in any value.

 

4. Control  Statements

suppose A=10;

if(A==10)

disp('ten')

else if( A==12)

disp('twelve')

else

disp('other value')

end

so there is a single end for set of if-else if-else operation. if you dont have else or else if, just have if-end block. No { like the one you use in C.

I will skip Switch here, to keep the topic simple.

 

5. Loops

Recall the index that you used while printing element of array? Same thing.

for(i=1:1:10)

disp(i);

end

in matlab ( and ) in for statement is not needed. But I prefer it to keep the code more simple. You may even write for i=1:1:10

while( a<10) that is an expression in the evaluation statement will do. 

end

6. Structures

In matlab you can create a structure variable directly and assign its fields

std.Name='Raj';

std.Age=20;

observe that Name is a string and is represented by ' '

if you want to create an array of structures?

std(1).Name='Raj'

std(1).Age=20;

 

std(2).Name='Sunanda'

std(1).Age=22;

simple.

7. Functions

7.1 Defining function.

Open an editor and just follow the following architecture

function [a b]=Operation( C,D)

and save the File. The file name must be same as function name and you can see in the save dialog the name appears is Operation. C D are any matrix and a, b are retrun type

remember all the return types must be assigned and no return statement is required in the function.

function [a,b]=Operation( C,D)

a=C+D;

b=C-D;

 

will be all that the file will contain.

7.2 Function calling

Either from command prompt or from another file just write

[a,b]=Operation(20,30);

Here a and b will hold the retrun values. instead of a and b you can give any name. instead of 20,30 you can pass any matrix.

 

That completes the stuff you require for basic Matlab coding. Do not trust me?

try out following Program

1)  Find if a given number is even or odd

2) Find the Factorial of a Given Number

3) Find the first n numbers of Fibonicci series 

4) Find if a number is Prime or not.

 

Hope Reading this tutorial was fun and you came close to learning how to start working with Matlab. Well Matlab is an Ocean but this initial techniques will give you the base for further work in matlab. Do write and tell me about your suggestions and comments

Tags: 

0
Your rating: None