You are on page 1of 5

Module I (matrix declaration and manipulation) This tutorial is about the fundamental programming element of MATLAB which is Array.

Array is the collection of number either in row or in column or in both row and column. According to arrangement of numbers in the array they are classified as following. 1) Scalar: If an array contains only one element then it is called Scalar.

2) Vector: If an array contains n number of element either in row or in column then that array is called as vector. Thus in MATLAB there are two types of Vectors a) Row Vector b) Column Vector

3) Matrix: If an array has n numbers arranged in the rectangular form in rows and column both than it is called matrix.

Declaration of Array: To declare array in the MATLAB command window different methods are available. All the possible syntax for declaring the arrays in MATLAB are tabulated follow.


Array Type 1 Scalar 2 Vector 1) Variable name = [a]


Variable name = *a b c d .+ (For row vector ) Variable name = *a; b; c; d.+ (For column vector)


Variable name = [m : q : n] m first term q spacing between consecutive term n last term


Variable name = linspace(xi , xf , n) xi Initial term xf Final term n Number of terms


pntAH = *a,b,c,d.+ (For row vector) pntAV = *a,b,c,d.+ (For column vector) Variable name = [a b c; d e f; g h I]


3 Matrix


2) 3) Note:

Variable name = [1st row elements;2nd row elements;3rd row elements] variable name =[linspace(xi,xf,n);linspace(xi,xf,n); linspace(xi ,xf ,n)]

1) Syntax for declaring the matrices are given in the fourth row of the table. 2) All above syntaxes are used to manipulate a matrix.


Lets have a look of the screen shots of an example

Steps: In step one matrix of order 36 is declared (matrix having 3 rows and 6 columns) In step two syntax a (4, :) signifies Elements of each column corresponding to 4th row. As in the above example there is no 4th row but using a (4, :) command 4th row is added to the matrix a and value is assigned to it according to the commands linspace (81, 100, 6) In the next step a (: , 6) signifies Elements of each row corresponding to 6th column and it is assigned [1;2;3;4] . Thus elements of each row in 6th column are replaced by 1, 2, 3 and 4.


In the next line a (4, 5) represents the element corresponding to the position 45 and value is assigned to it as [100]. Thus 96.2000 in the a is replaced by 100.0000

Lets one more example of the following screen shot

Another matrix b is declared with the command b=[linspace(2,10,6);linspace(11,18,6);linspace(21,30,6);linspace(31,40,6)]


In the next step command a (4, 4) = [b (4, 4)] is used which signifies the element in matrix a (declared in previous example) at position 44 is assigned the value of the element at position 44 in the matrix b. Thus 92.4000 in the matrix is replaced by 36.4000.

Next command a(:,2)= [b(:,2)] signifies the each elements of each row corresponding to the 2nd column in matrix a is assigned the value [b(:,2)] ,where b(:,2) means the elements of each row corresponding to the 2nd column in matrix b. Thus 2nd column is the matrix a is replaced with the values of 2nd column of matrix b.

Important note After each command matrix a get updated and the following commands are applied on that updated matrix only.