Matlab Tutorial: Array and Matrix Basic Operations

Hello readers! After breaking for a long time (because of thesis and work), finally I decide to write a post again. Starting from this post, I’ll use English just to improve my English writing skill. If you find some grammatical errors here, don’t hestitate to remind me, i need your feedback. Well, I’ll discuss the basic operation of array and matrix in Matlab in this post. I hope this tutorial can help readers (especially my friends who utilize Matlab for their thesis project) understand the basic operation of Matrix in Matlab easily.

Matlab (an abbreviation for “Matrix Laboratory”) is designed to operate primarily on whole matrices and arrays1. Therefore, understand the basic operation of matrices and arrays in Matlab is urgently required. Before explaining the operations, we should know what’s the differences between array, vector and matrix. Basically, all Matlab variables are multidimensional arrays, no matter what type of data. A vector in Matlab is defined as an array which has only one dimenstion with a size greather than one2.. On the other hand, a matrix is a two-dimensional array NxM. The samples of vector and matrix can be seen in Figure 1.

image

Figure 1. The samples of horizontal vector ( a ), vertical vector ( b ) and matrix ( x )

Next, the operations of arrays and matrices are as follows:

Create horizontal vector

To create an array with n elements in a single row (horizontal vector), just separate the elements with either a comma (,) or a space as seen in Figure 1 sample a.

Create vertical vector

To create an vertical vector, separate the element with semicolon ( ; ) as seen in Figure 1 sample b.

Create matrix

To create matrix n x m, separate the element in column with comma or space and separate the element in row with semicolon. We can see the sample in Figure 1 ( x ).

Accessing element of a matrix

For 1xm matrix (horizontal vector), we can access the element i of array by placing index number i through the operator (). Figure 2 shows the sample of accessing the second index of matrix T (1 x 5 dimension).

image

Figure 2. Sample of accessing the second index of a matrix T

On the other hand, for accessing an element of matrix nxm, we can use the (r,s) subscript, where r is the index in the row, and s in the column. Figure 3 shows the sample of this operation. Matrix X has 2 x 3 dimension, then we try to access the element that places on second row , second column. So, the code is X(2,2).

image

Figure 3. Sample of accessing matrix element in multidimensional array

Transposed matrix

Transpose is used to convert rows to columns (or columns to rows). We just add a single apostrophe (‘) to add this operation. Figure 4  shows the sample of using transpose in Matrix s. In the beginning, matrix s has 2 x 3 dimension. After transpose, matrix s has 3 x 2 dimension.

image

Figure 4. Transposed matrix

Special matrices

Matlab provides some special matrices, such as zeros, ones and eye. Zeros means Matlab generates all entries with 0, ones means Matlab generates all entries with 1 and eye means Matlab generates 1 on diagonal elements and all other elements are zero. These special matrices can be useful when we create a vector label for classification (just example). The sample code below shows the using special matrices (zeros and ones) for classification problem.

labels = [-ones(size(allfeats_neg’,1),1); zeros(size(allfeats_neu’,1),1);ones(size(allfeats_pos’,1),1)];

The code above explains that all negative features are labeled by –1  (adding negative symbol before ones), all neutral features are labeled with 0 and all positives features are labeled by 1.

Well, this is the end of this post. I’ll explain more about classification and maybe cross validation in the next post. See you.

 

References

1. http://www.mathworks.com/help/matlab/learn_matlab/matrices-and-arrays.html

2. https://en.wikibooks.org/wiki/MATLAB_Programming/Arrays/Basic_vector_operations

Advertisements