You are on page 1of 9

York University ITEC2600 By Amanda Tian

3.3 adding/deleting/modifying vector elements


The following examples shows how to add, delete and modify elements of a vector.
>> v = [1, 2, 3, 4] % initial a vector

v=
1 2 3 4

>> v(2) = 5 % change the 2nd element of v to 5

v=
1 5 3 4

>> v([1,4]) = [6, 7] % change the 1st and last elements of v to 6, 7

v=
6 5 3 7

>> v = [v, 8, 9] % add 8, 9 to v

v=
6 5 3 7 8 9

>> v([2,4]) = [] % remove the 2nd and 4th elements of v

v=
6 3 8 9

4. Initial and create matrix, and matrix operation


4.1 create matrix.
There are multiple functions we can use to create matrix. The following examples lists some of those.

>> A = [1 2 3; 4 5 6; 7 8 9] % create a 3 by 3 matrix. We use ; separate rows.

A=
1 2 3
4 5 6
7 8 9

>> A = zeros(3,4) % a 3 by 4 matrix with 0's

A=
0 0 0 0
0 0 0 0
0 0 0 0

>> A = rand(3,2) % a 3 by 2 matrix with random numbers (following uniform distribution)

A=
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975

>> A = ones(3,3) % a 3 by 3 matrix with 1's

A=
1 1 1
1 1 1
1 1 1

>> A = repmat(7, 2,5) % a 2 by 5 matrix with a fixed number everywhere.


York University ITEC2600 By Amanda Tian

A=
7 7 7 7 7
7 7 7 7 7

>> A = eye(6) % identity matrix of 4 by 4

A=
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1

We can also create a matrix by combining vectors. If we use ; to separate two vectors, then two vectors will be arranged row by
row. If we use space to separate two vectors, then two vectors will be arranged side by side (column by column). Note that we
can combine row vectors row by row, and we can combine two column vectors side by side (column by column). If you would
like to put two row vector side by side, use transpose ‘ to rotate the resulted matrix. See the following examples.

>> a = [1 2 3] % create a row vector

a=
1 2 3

>> b = [5 1 4] % create a row vector

b=
5 1 4

>> c = [3 2 8] % create a row vector

c=
3 2 8

>> A = [a; b] % matrix by combining two vectors row by row

A=
1 2 3
5 1 4

>> A = [a; b]' % transpose to get a side by side (column by column) matrix

A=
1 5
2 1
3 4

>> A = [a; b; c]' % a 3 by 3 matrix, combine three vectors column by column

A=
1 5 3
2 1 2
3 4 8

After transpose, the resulted a' and b' are two column vectors. Hence we can combine them side by side. Space is used as
separator instead of ;
>> A = [a' b'] % another way to combine vectors side by side.

A=
1 5
2 1
3 4
York University ITEC2600 By Amanda Tian

>> A = [a' ; b'] % result a 6 by 1 matrix/vector

A=
1
2
3
5
1
4

>> A = [a , b] % result a 6 by 1 matrix/vector

A=
1 2 3 5 1 4

4.2 review elements of matrix.


When we want to view or check some elements (subset) of a matrix. We use ( ) to specify the range (location) of the subset.
Comma , is used to separate the row and column indices. Row/column index can be number or a numeric vector. For example:

>> A(1 , 2) % first row, 2nd column

ans =

>> A( : ,3) % 3rd column, : indicate all elements in that row/column

ans =
3
2
8

>> A(2 , :) % 2nd row

ans =
2 1 2
>> A(2 , [1 3]) % 2nd row,first and 3rd column

ans =
2 2

>> A( : , [1 3] ) % first and 3rd column

ans =
1 3
2 2
3 8

4.3 matrix operation, elementary level operation vs matrix level operation.


In Matlab, if dot sign . is put before the operation sign, usually it indicates elements level operation. Note that addition and
subtraction (+ and -) are only elements level operations.

>> A = [1 2; 3 4] % create a matrix

A=
1 2
3 4

>> B = [2 3; 4 5] % create a matrix

B=
York University ITEC2600 By Amanda Tian

2 3
4 5

>> A + B % elementary level or matrix level

ans =
3 5
7 9

>> A - B % elementary level or matrix level

ans =
-1 -1
-1 -1

Matrix level multiplication for our example matrix A and B:

Element level multiplication for our example matrix A and B:

Element level division for our example matrix A and B:

Matrix level square for our example matrix A:

Element level square for our example matrix A and B:

>> A * B % matrix level multiplication

ans =
10 13
22 29

>> A .* B % elementary level multiplication

ans =
2 6
12 20

>> A ./ B % elementary level division

ans =
0.5000 0.6667
0.7500 0.8000

>> A ^ 2 % matrix level, equivalent to A*A. must be square matrix

ans =
7 10
15 22

>> A .^ 2 % elementary level, square each element of matrix


York University ITEC2600 By Amanda Tian

ans =
1 4
9 16

>> A .^ B % elementary level

ans =
1 8
81 1024

4.4 operation between matrix and vector

>> A = [1 2 ; 3 4] % a 2 by 2 matrix

A=
1 2
3 4

>> v = [2 2] % a 1 by 2 row vector

v=
2 2

>> u = [2 ; 2] % a 2 by 1 column vector

u=
2
2

>> A * v % 2 by 2 * 1 by 2, inner dimensions do not match, get error


Error using *
Inner matrix dimensions must agree.

>> A * v' % 2 by 2 * 2 by 1 = > 2 by 1

ans =
6
14

>> v * A % 1 by 2 * 2 by 2 = > 1 by 2

ans =
8 12

>> v'* A % 2 by 1 * 2 by 2, inner dimensions do not match, get error


Error using *
Inner matrix dimensions must agree.

>> A * u % 2 by 2 * 2 by 1 => 2 by 1

ans =
6
14

>> A * u' % 2 by 2 * 1 by 2, inner dimensions do not match, get error


Error using *
Inner matrix dimensions must agree.

>> u * A % 2 by 1 * 2 by 2, inner dimensions do not match, get error


York University ITEC2600 By Amanda Tian

Error using *
Inner matrix dimensions must agree.

>> u'* A % 1 by 2 * 2 by 2 => 1 by 2

ans =
8 12

To solve A * x = b linear equation, we use backwards slash sign ( \ ) . A is a m by n matrix, b is a n by 1 column vector.

>> A = [1 0 3;2 4 1] % a 2 by 3 matrix

A=
1 0 3
2 4 1

>> b = [3;9] % a column vector

b=
3
9

>> x = A \ b % solution of equation A * x = b.

x=
0
2
1

4.5 other functions for matrix


>> A = [1 2; 3 4]

A=
1 2
3 4
>> B = [1 1; 1 1]

B=
1 1
1 1

>> C = [A B] % combine two matrix by column

C=
1 2 1 1
3 4 1 1

>> C = [A; B] % combine two matrix by row

C=
1 2
3 4
1 1
1 1

>> sum(A, 1) % column sum

ans =
4 6

>> sum(A, 2) % row sum


York University ITEC2600 By Amanda Tian

ans =
3
7

>> mean(A, 1) % column mean

ans =
2 3

>> mean(A, 2) % row mean

ans =
1.5000
3.5000

>> [nrow ncol] = size(C) % dimension of matrix, nrow is the number of rows, ncol is the number of columns

nrow =
4

ncol =
2

>> inv(A) % inverse matrix of A. A must be square matrix

ans =
-2.0000 1.0000
1.5000 -0.5000

>> eig(A) % eigenvalues of A

ans =
-0.3723
5.3723

>> [V, D] = eig(A) % D matrix with eigenvalues on diagonal; V matrix of eigenvectors

V=
-0.8246 -0.4160
0.5658 -0.9094

D=
-0.3723 0
0 5.3723
York University ITEC2600 By Amanda Tian

4.6 Matrix operations formula


A = [𝑎𝑖𝑗 ]
B = [𝑏𝑖𝑗 ]
C = [𝑐𝑖𝑗 ]

Operations Type Example Formula


+ Elementary C=A+B 𝑐𝑖𝑗 = 𝑎𝑖𝑗 + 𝑏𝑖𝑗
- Elementary C=A+B 𝑐𝑖𝑗 = 𝑎𝑖𝑗 − 𝑏𝑖𝑗
𝑚
* Matrix C=A*B
A: n by m 𝑐𝑖𝑗 = ∑ 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗
B: m by p 𝑘=1
𝑚
^ Matrix C = A ^2 = A*A
A: m by m 𝑐𝑖𝑗 = ∑ 𝑎𝑖𝑘 ∗ 𝑎𝑘𝑗
𝑘=1
\ Matrix A*x= b Solve the equation for x.
.* Elementary C=A.*B 𝑐𝑖𝑗 = 𝑎𝑖𝑗 ∗ 𝑏𝑖𝑗
./ Elementary C = A ./ B 𝑐𝑖𝑗 = 𝑎𝑖𝑗 / 𝑏𝑖𝑗
.^ Elementary C = A .^ 2 𝑐𝑖𝑗 = 𝑎𝑖𝑗 ^2
C = A .^ B 𝑐𝑖𝑗 = 𝑎𝑖𝑗 ^𝑏𝑖𝑗

5. Strings and cell array.

5.1 string vector and string cells.


There are different string/character objects in Matlab. We will mainly illustrate three types:
• Character vector/array: defined using single quotation ‘’ or along with []
• String cell: defined using single quotation ‘’ and {}
• String vector: defined using double quotation “” and []

Here is some examples about defining character vector/array. We can use size() and whos to check the length and
type of the variables, ‘whos’ function provide more detailed information about the variable.

>> s1 = 'my name is peter' % a string or a character vector/array.

s1 =
'my name is peter'

>> s2 = [ 'my' 'name' 'is' 'peter' ] % combine strings horizontally.

s2 =
'mynameispeter'

>> s3 = ['my' ' name' ' is' ' peter'] % add space before each words

s3 =
'my name is peter'

>> s4 = ['my', ' ', 'name', ' ', 'is', ' ', 'peter'] % can also add space this way.

s4 =
'my name is peter'

>> size(s1) % check the size of s, a character vector of length 16

ans =
1 16
York University ITEC2600 By Amanda Tian

>> whos s1 % get the brief information about s1

Name Size Bytes Class Attributes


s1 1x16 32 char

Note that, when using [] to combine character vectors, we can also combine variables. For the following example,
we set the name variable with value ‘Merry’, then use [] to combine it with other characters.

>> name = ' Merry'

name =
' Merry'

>> s = [ 'my' ' name' ' is' name ] % combine strings horizontally.

s=
'my name is Merry'

Examples about viewing elements of a character vector, and changing the values .

>> s(1) % view the first character

ans =
'm'
>> s(2:end) % view the 2nd until the last characters.

ans =
'y name is Merry'

>> s(4:6) = 'abc' % replace the value of the 4th, 5th, and 6th characters .

s=
'my abce is Merry'

You might also like