Professional Documents
Culture Documents
v=
1 2 3 4
v=
1 5 3 4
v=
6 5 3 7
v=
6 5 3 7 8 9
v=
6 3 8 9
A=
1 2 3
4 5 6
7 8 9
A=
0 0 0 0
0 0 0 0
0 0 0 0
A=
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975
A=
1 1 1
1 1 1
1 1 1
A=
7 7 7 7 7
7 7 7 7 7
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
b=
5 1 4
c=
3 2 8
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=
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=
1
2
3
5
1
4
A=
1 2 3 5 1 4
ans =
ans =
3
2
8
ans =
2 1 2
>> A(2 , [1 3]) % 2nd row,first and 3rd column
ans =
2 2
ans =
1 3
2 2
3 8
A=
1 2
3 4
B=
York University ITEC2600 By Amanda Tian
2 3
4 5
ans =
3 5
7 9
ans =
-1 -1
-1 -1
ans =
10 13
22 29
ans =
2 6
12 20
ans =
0.5000 0.6667
0.7500 0.8000
ans =
7 10
15 22
ans =
1 4
9 16
ans =
1 8
81 1024
>> A = [1 2 ; 3 4] % a 2 by 2 matrix
A=
1 2
3 4
v=
2 2
u=
2
2
ans =
6
14
>> v * A % 1 by 2 * 2 by 2 = > 1 by 2
ans =
8 12
>> A * u % 2 by 2 * 2 by 1 => 2 by 1
ans =
6
14
Error using *
Inner matrix dimensions must agree.
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
b=
3
9
x=
0
2
1
A=
1 2
3 4
>> B = [1 1; 1 1]
B=
1 1
1 1
C=
1 2 1 1
3 4 1 1
C=
1 2
3 4
1 1
1 1
ans =
4 6
ans =
3
7
ans =
2 3
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
ans =
-2.0000 1.0000
1.5000 -0.5000
ans =
-0.3723
5.3723
V=
-0.8246 -0.4160
0.5658 -0.9094
D=
-0.3723 0
0 5.3723
York University ITEC2600 By Amanda Tian
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'
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'
ans =
1 16
York University ITEC2600 By Amanda Tian
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'
>> 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 .
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'