Matlab : Matrix

In this chapter


Matrix Declaration

If we have matrix x = [1 2 3

4 5 6]

In Matlab, matrix’s column is separated with comma “,” or space ” ” and the row is separated with semicolon “;”

Syntax :

>> x = [1, 2, 3; 4 5 6;]

Result :

x =

1 2 3
4 5 6


Indexing a matrix

Matrix in Matlab is constructed row wise as shown below :

Suppose we have matrix A = [1, 2; 3, 4]. So the index will be:

A(1) will address to 1

A(2) will address to 2 not 3

A(3) will address to 3 not 2

A(4) will address to 4


Get element matrix

The element of a matrix 2D is in A(index of row, index of column) and for matrix 3D is A(index of row, index of column, index of dimension) and so on.

Suppose we have matrix B = [4, 2, 5; 7, 8, 11; 2, 0, 9]. So to get 0 we use B(3, 2)


Get sub matrix

To extract sub matrix from a matrix we can use interval as defined below :

Suppose we have matrix C = [1, 2, 3; 4, 5, 6; 7, 8, 9]. To extract the second row from matrix C we use :

subC = C(2,:); or C(2,1:3);

The colon operator means interval (see for loop in previous chapter) so if we write 1:3 then it means 1 to 3.

To extract 5, 6, 8, 9 we can use

subC = C(2:3, 2:3);


Special matrix function

Matlab provides a function to generate matrix with predefined value :

D = zeros(3, 3);

That syntax will create matrix D with size 3×3 and 0 for all its element

The other function is ones(m, n), eye(n), etc


Matrix operation

The operator + (addition), – (subtraction), * (multiply), / (divide) can also be used in matrix.

Suppose we can use + in A + B where A and B is matrix to perform addition matrix A and B

The multiply “*” operator performs normal matrix multiplication but if we add dot in front of the operator, it will perform element wise operation.

Suppose we have two matrix A and B and perform element wise multiplication on them

C = A .* B (element wise multiplication)


find() function

The find() function is useful for extracting value in the matrix which match the given condition.

Suppose we have matrix A = [1, 2, 3; 4, 5, 6; 7, 8, 9] and we want to find any value in the matrix which larger than 5.

>> find(A > 5)

ans =

3
6
8
9

The find() function return linear index of the matrix which contain the result of condition. As in the example the index value is A(3) = 7, A(6) = 8, A(8) = 6, A(9) = 9


reshape() function

Matrix dimension can be manipulated using reshape() function. For the example :

Suppose we have matrix 3 x 3 in A = [1, 2, 3; 4, 5, 6; 7, 8, 9] and we want to reshape the matrix into 1 x 9. We can use :

reshape(A, 1, 9)

where A is the matrix, 1 is the number of row and 9 is the number of column


min() and max() function

To extract minimum value or maximum value of matrix we can use min() and max() function. As default Matlab return the minimum or maximum value in column wise so the value is the minimum or maximum from every column


Saving matrix into Excel format

We can use xlsread() and xlswrite() to read excel data and write matlab data to excel format

[numeric, text, raw] = xlsread(file, sheet, range);

We can define the output as numeric or text or raw which extract all information in the excel

Suppose we have excel file :

untitled-2

Syntax :

[numeric text raw] = xlsread(‘Book1.xlsx’,’Sheet1′);

Result :

untitled-3

To extract specific range from the excel sheet we can provide the third input parameter. Suppose we want to extract data from A1 to A3 so we can write :

[numeric text raw] = xlsread(‘Book1.xlsx’,’Sheet1′,’A1:A3′);

Write data to excel

Suppose we have matrix A = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10]. We can write the matrix into excel format. From this example we will write matrix A into Sheet2 in our excel file

Syntax :

A = [1,2,3,4,5;6,7,8,9,10];
xlswrite(‘Book1.xlsx’,A,’Sheet2′);

Result : 

untitled-4


Saving matrix into .mat