The Transpose Operator
The Transpose Operator
C = 2 55 14 8
21 5 32 11
41 64 9 1
>> D=C'
D = 2 21 41
55 5 64
14 32 9
8 11 1
1
(Vectors Arithmetic 2)
Addition or subtraction of matrices is possible as long as they
are the same size.
>> [1 2 3]+[4 5 6]
ans = 5 7 9
q=m.*p
5
(More about Matrices)
You can define matrices the same way by combining rows
and columns.
>> mat1 = [1 2;3 4]
mat 1 = 1 2
3 4
>> mat2 = [mat1;mat1]
mat2 = 1 2
3 4
1 2
3 4
6
(The .^ and sqrt Commands)
.^ us used for the element-by-element power operator
(works like .* and ./ seen in slide 25).
>> [1 2 3].^2
ans = 1 4 9
Square roots are done with sqrt (works with negative
numbers too!).
>> sqrt (-9)
ans = 0 + 3.0000i
>> sqrt ([1 2 4 9])
ans = 1.0000 1.4142 2.0000 3.0000
7
(Trig Commands)
Trig functions are supported like sin.
>> sin ([pi/4 pi/2 pi])
ans = 0.7071 1.0000 0.0000
A few more command examples... There are many many
more!
log(x), log10(x), cos(x), atan(x), exp(x), round(x),
floor(x), ceil(x), angle(x), abs(x)...
They work exactly like the math library functions in C.
Need help with the sin function? Try help sin or doc sin.
8
(Saving and Loading Variables)
Use save to save variables to a file.
>> save myfile mat1 mat2
saves matrices mat1 and mat2 to the file myfile.mat
9
(More about Transpose)
The transpose operators turn a column vector into a row
vector and vice versa as seen before. For example to
transpose a vector x,
10
(Example: Scalar Product)
A scalar product can be done with one row vector
by a column vector. A transpose may be required.
11
(Multiple Initializations)
It is possible to perform multiple initializations at once:
12
(Index vs. Subscript)
For a vector, indexes and subscripts are the same.
Remember that indexes and subscripts start at 1 in
MATLAB.
For matrices, subscripts have the row and column
numbers separated by a comma. Indexes, on the other
hand, indicate the linear position from the start of the
matrix.
subscripts indexes
13
(Matrix Indexing)
The index argument can be a matrix. In this case,
each element is looked up individually, and
returned as a matrix of the same size as the index
matrix.
14
(Working Whole Rows or Columns)
The colon operator selects rows or columns of a matrix.
>> c = b (1,:)
c = 10 20 40
>> d = b (:,2)
d = 20 40
15
(Colon Operator)
The colon : lets you address a range of elements
• Matrix
• Matrix
19
Extracting Data
The colon represents an entire row or column when used in as an array
index in place of a particular number.
n = n(:,1) n(2,:)
2 50 20 ans =
1 1 2 ans = 1 1 2
2
0 55 66
1
The colon operator can also be used to extract 0
a range of rows or columns:
n(2:3,:) n(1,2:3)
ans = ans =
1 1 2 50 20
0 55 66
20
(Finding Minimum and Maximum Values)
>> vec = [10 7 8 13 11 29];
To get the minimum value and its index:
21
(Getting Specific Columns)
You can replace vector index or matrix
indices by vectors in order to pick out
specific elements. For example, for
vector v and matrix m
• v([a b c:d]) returns elements a, b, and
c through d
• m([a b],[c:d e]) returns columns c
through d and column e of rows a and b
22
(Colon Operator Examples 1)
>> v=4:3:34
v = 4 7 10 13 16 19 22 25 28 31 34
A = 10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
B = 10 8 6 5 4
2 6 10 12 14
24
Example?
A=
10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
B=A([1 3],:)
B= B=A([1 3])
10 9 8 7 6 5 4 B=
2 4 6 8 10 12 14 10 2
B=A(:,[1 3 5:7])
B=A([1 3],[1 3 5:7])
B=
B=
10 8 6 5 4
10 8 6 5 4
1 1 1 1 1
2 6 10 12 14
2 6 10 12 14
0 0 0 0 0
25
(Two ways to add elements to existing variables)
Assign values to indices that don't exist:
MATLAB expands array to include indices, puts the specified
values in the assigned elements, fills any unassigned new
elements with zeros.
Add values to ends of variables:
Adding to ends of variables is called appending or
concatenating.
"end" of vector is right side of row vector or bottom of
column vector.
"end" of matrix is right column and bottom row.
26
(Assigning to undefined indices of vectors)
>> fred=1:4
fred = 1 2 3 4
>> fred(5:10)=10:5:35
fred = 1 2 3 4 10 15 20 25 30 35
>> wilma=[5 7 2]
>> wilma(8)=4
wilma = 5 7 2 0 0 0 0 4
>> barney(5)=24
28
>> pebbles = [3 8 1 24];
betty = 3 8 1 24 4 7 10 13 16
bedrock = 3
8
1
24
4
7
10
13
29
16
(Appending to matrices)
• If appending one matrix to the right side of
another matrix, both must have same
number of rows.
• If appending one matrix to the bottom of
another matrix, both must have same
number of columns
30
>> A2=[1 2 3; 4 5 6]
A2 = 1 2 3
4 5 6
>> B2=[7 8; 9 10]
B2 = 7 8
9 10
>> C2=eye(3)
C2 = 1 0 0
0 1 0
0 0 1
31
>> Z=[A2 B2]
Z = 1 2 3 7 8
4 5 6 9 10
Z = 1 2 3
4 5 6
1 0 0
0 1 0
0 0 1
32
To delete elements in a vector or matrix, set range
to be deleted to empty brackets.
>> kt=[2 8 40 65 3 55 23 15 75 80]
kt = 2 8 40 65 3 55 23 15 75 80
>> kt(6)=[] Delete sixth element (55)
kt = 2 8 40 65 3 23 15 75 80 55 gone
33
To delete elements in a vector or matrix, set
range to be deleted to empty brackets.
>> mtr=[5 78 4 24 9; 4 0 36 60 12; 56 13 5 89
3]
mtr = 5 78 4 24 9
4 0 36 60 12
56 13 5 89 3
mtr = 5 9
4 12
34 56 3
MATLAB has many built-in functions for working
with arrays. Some common ones are:
• length(v) - number of elements in a vector.
• size(A) - number of rows and columns in a
matrix or vector.
• reshape(A,m,n) - changes number of rows
and columns of a matrix or vector while keeping
total number of elements the same. For example,
changes 4x4 matrix to 2x8 matrix.
35
reshape Example
>> a = [1 2 ; 3 4]
a=
12
34
>> a = [a [5 6; 7 8]]
a= ans =
1256 1
3478
3
>> reshape (a, 1, 8) 2
ans = 4
13245768 5
7
>> reshape (a, 8, 1)
6
8
36
• diag(v) - makes a square matrix of zeroes with
vector in main diagonal.
• diag(A) - creates vector equal to main diagonal
of matrix.
37
Example$
>> a=[1 2 3]
>> B= [1 2 3;2 5 7; 0 0 8]
a=
B=
1 2 3
1 2 3
2 5 7
>> diag(a)
0 0 8
ans =
>> diag(B)
1 0 0
ans =
0 2 0
0 0 3
1
5
8
38