These five symbols are the most important in MATLAB. They have several uses
and are used in conjunction with one another. The summary is grouped by symbol,
whereas the example section is grouped by function.

NOTE: In order to make the symbols stand out, space has been left between symbols throughout this guide. This is not necessary when actually using MATLAB, but neither does it affect the result.

**The square bracket [ ]**To build matrices: A = [1 2 ]

**The parentheses ( )**To indicate functions: B = eig(C)

To extract elements:

b = A(1 , 2) for matrices

b = a(3) for vectorsTo redefine elements: A(1 , 2) = -5

**The semicolon ;**To separate the rows of a matrix: A = [1 2 ; 3 4]

To place submatrices under one another: C = [A ; B]

To suppress printing: A = [1 2 3 4];**The comma ,**To place matrix blocks side by side: B = [vec1 , vec2]

To extract elements of a matrix: a = C(2 , 3)

To separate functions on a line: a = 1 , B = eig(C)**The colon :**1 to 4 by steps of 1: 1 : 4

-1 to 2 by steps of .5: -1 : .5 : 2

5 to 1 in decreasing steps of 1: 5 : -1 : 1

**The use of [ ] to indicate the building of a matrix. The use of ; and , as matrix separators.**When we enter the elements of a matrix, we use square brackets, and separate the rows (which can be thought of as row vectors) by a

*semicolon*.**The use of ( ) as a function indicator.**If we wanted to find the rank of C, we would use the rank function rank( ). The dependent variable is placed inside the parentheses.

>> rank_C = rank(C)

rank_C =

**The use of ( ) and , to extract or redefine an element.**To find the element in row 2 and column 3 of A above we use parentheses which is the extraction function indicator and then enter the row and column values separated by the comma:

>> b = A(2 , 3) % row 2 , column 3

6If a is a vector then all we have to do is give the element number:

>> a = [1 3 5];

>> b = a(2)

3On the other hand suppose that we wanted to change the element in row 2 and column 3 so that it is -9, while leaving all the other elements alone. There is no need to retype all of A. We just specify the new value.

>> A(2,3) = -9 % redefine row 2, column 3

A =

>> A = [1 2 3 ; 4 5 6] % two vectors on separate lines

A =

1 2 3

4 5 6

>> B = [7 8 9 ; 10 11 12]

B =

7 8 9

10 11 12

We can now join the two matrices A and B in two ways to form a new matrix:

We can put B underneath A by using a *semicolon*.

>> C = [A ; B] % place submatrix B under A

C =

1 2 3

4 5 6

7 8 9

10 11 12

We can put A and B next to one another by using a *comma*.

>> D = [A , B]

D =

1 2 3 7 8 9

4 5 6 10 11 12

2

1 2 3

4 5 -9The use of the colon to extract submatrices is discussed in subsection v, below and in section II.

**The use of : to indicate the "range" of indices.**The

*colon*could be loosely translated by the term "range". Thus if we wanted a row vector with the integer range -1 to 4 we write:>> x = [-1 : 4]

-1 0 1 2 3 4

Note that since we are building a vector we use brackets,

**[ ]**.If we wanted to go from -1 to 2 by steps of .5, we would include the .5 in the statement. Note that there are now two

*colons*.>> x_step = [-1 : .5 : 2]

-1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000

To generate a vector in reverse order use negative steps.

>> X_dec = [5 : -1 : 1]

X_dec =

5 4 3 2 1

[Note: Writing [5 : 1] would give the empty matrix [ ]; see section II.2.vi].

For examples of the use of the colon, see Section II.1, example 1 and Section II.4

**The use of : when extracting submatrices.**Subsection iv illustrated the used of the

*colon*as a range indicator. By combining the colon with the extraction function of subsection iv, we can extract rows, columns and submatrices.To extract the second row of

*A*we fix the row value at 2 and let the column values range over all values as indicated by the colon.>> row_2 = A(2 , : ) % the entire column range in row 2

4 5 6

To extract the third column of

*A*we fix the column value at 3 and let the row values range over all values as indicated by the colon.>> col_3 = A( : , 3) % the entire row range in column 3

3

6If we only wanted to extract columns 2 through 3 of row 2 of A we would limit the column range by use of [2 : 3]

*>> row_2_shortened = A(2 , [2 : 3]) % row 2, columns 2 to 3**5 6*We could also write:

*>> indices = [2 : 3]**2 3*and then

*>> row_2_shortened = A(2 , indices)**5 6*More examples of the use of the colon are given in section II.4 which deals with extracting and building matrices.

**The use of ; to suppress printing.**We reenter B, but this time we put a semicolon after the bracket to suppress printing. Note that the semicolon is also used inside the brackets to separate the rows:

>> B = [7 8 9 ; 10 11 12] ; % asemicolo outside to suppress printing

To print B we simply type:

>> B

B =

7 8 9

10 11 12To print just the matrix without "B = " we would type

>> disp(B) % see section I.5.i ].

To suppress printing several variables in a row we put a semicolon after each:

>> B; C;

**Using , to place commands on the same line.**If we wanted to find the ranks of C and D, we could write the two commands on separate lines or use a

*comma*.>> rank_C = rank(C) , rank_D = rank(D)

rank_C =

2

rank_D =

2

Web Site Training provided by Kathryn Charis Apunen Washburn summer 2001