792 Introduction to MATLAB
C.2 Defining Matrices in MATLAB
Before performing arithmetic operations or using them in developing MATLAB pro-
grams or m-files, the relevant matrices need to be defined using statements such as the
following.
1.A row vector or 1×nmatrix, denotedA, can be defined by enclosing its
elements in brackets and separated by either spaces or commas.
Example: A=[1 2 3]
2.A column vector orn×1 matrix, denotedA, can be defined by entering its
elements in different lines or in a single line using a semicolon to separate them
or in a single line using a row vector with a prime on the right-side bracket (to
denote the transpose).
Example: [1
A= 2 , A=[1; 2 ; 3],orA=[1 2 3]′.
3 ]
3.A matrix of sizem×n, denotedA, can be defined as follows (similar to the
procedure used for a column vector).
Example: [1 2 3
A=4 5 6 ,orA=[1 2 3; 4 5 6; 7 8 9].
7 8 9]
4.Definitions of some special matrices:
A=eye( 3 )
implies an identity matrix of order 3: A=
1 0 0
0 1 0
0 0 1
.
A=ones( 3 )
implies a square matrix of order 3 with all elements equal to one:A=
1 1 1
1 1 1
1 1 1
.
A=zeros( 2 , 3 )
implies a 2×3matrix with all elements equal to zero: A=
[
0 0 0
0 0 0
]
.
5.Some uses of the colon operator (:):
(i) To generate all numbers between 100 and 50 in increments of− 7
> >100 :−7 : 50
This command generates the numbers 100 93 86 79 65 58 51