§1 Basic ConceptsSome ShortcutsSome TipsNumerical MatricesVariablesArithmetic Expressions and Mathematical Functions§2 Matrix OperationsBasic OperationsMatrix Functions and OperatorsMatrix Creation
xxxxxxxxxx
71exit,quit % quit
2Ctrl-C % interruption
3Ctrl-l % moving one word to the left
4Ctrl-r % .......................right
5Ctrl-a % moving to the beginning of the line
6Ctrl-e % ..............end..................
7Ctrl-k % deleting from here to the end of the line
You are allowed to define more than one variable on the same line, provided they are separated by ';'.
You can input '...' at the end of the line in order to continue typing on the next line.
In MATLAB, The elements of the matrix are indexed by column first and then row.
Date Formats
Numbers | Forms (BACK) |
---|---|
0 | 22-Apr-2020 02:14:00 |
1 | 22-Apr-2020 |
2 | 04/22/20 |
3 | Apr |
4 | A (April) |
5 | 4 (April) |
6 | 04/22 |
7 | 22 |
8 | Thu (Thursday) |
9 | T (Thursday) |
10 | 2020 |
11 | 20 (2020) |
12 | Apr20 |
13 | 02:14:00 |
14 | 02:14:00 AM |
15 | 02:14 |
16 | 02:14 AM |
17 | Q2-20 ( quarters of 1 hour-2020) |
18 | Q2 |
Output Formats
Normally, integer or short float with 4 decimal places
Format Changes
xxxxxxxxxx
581>> format
2>> p=1/3-1
3
4p =
5
6 -0.6667
7
8>> format short;p
9
10p =
11
12 -0.6667
13
14>> format long;p
15
16p =
17
18 -0.666666666666667
19
20>> format short e;p
21
22p =
23
24 -6.6667e-01
25
26>> format long e;p
27
28p =
29
30 -6.666666666666667e-01
31
32>> format hex;p
33
34p =
35
36 bfe5555555555556
37
38>> format +;p
39
40p =
41
42-
43
44>> format bank;p
45
46p =
47
48 -0.67
49
50>> format rat;p
51
52p =
53
54 -2/3
55
56>> format compact;p
57p =
58 -2/3
Help Commands
2-dim Matrices
row: separated by ' ' or ','
column: separated by ';' or Enter
xxxxxxxxxx
101% e.g.
2>> A=[1,2,3;4,5,6;]
3% or
4>> A=[1 2 3
54 5 6]
6
7A =
8
9 1 2 3
10 4 5 6
xxxxxxxxxx
101% You can also assign to the elements of a matrix one by one.
2>> B ( 1,1 ) = 1;
3B ( 1,2 ) = 7;
4B ( 2,1 ) =-5;
5B ( 2,2 ) = 0
6
7B =
8
9 1 7
10 -5 0
Multi-dim Matrices
xxxxxxxxxx
311% e.g.1 constructed from 2 known 2-dim matrices
2>> A=[1 2 3
34 5 6];
4>> B=[11 12 13
514 15 16];
6>> C(:,:,1)=A;
7>> C(:,:,2)=B
8
9C(:,:,1) =
10
11 1 2 3
12 4 5 6
13
14
15C(:,:,2) =
16
17 11 12 13
18 14 15 16
19% e.g.2 change one element of the above C
20>> C(1,1,1)=100
21
22C(:,:,1) =
23
24 100 2 3
25 4 5 6
26
27
28C(:,:,2) =
29
30 11 12 13
31 14 15 16
Dimensions and Lengths
size(#)
xxxxxxxxxx
241% size(Matrix_name): giving the length of every dim
2>> size(A)
3
4ans =
5
6 2 3
7% size(Matrix_name,Dim_number): giving the length of the specific dim
8>> size(A,2)
9
10ans =
11
12 3
13% size(Row_vector_name/Column_vector_name):returning 1 & the length of the row vector / the length of the column vector & 1
14>> X=[1 2 3 4 5]
15
16X =
17
18 1 2 3 4 5
19
20>> size(X)
21
22ans =
23
24 1 5
length(#)
xxxxxxxxxx
121% length(Vector_name):giving the length of the vector
2>> length(X)
3
4ans =
5
6 5
7% length(Matrix_name):giving the maximum of the length of each dimension of the matrix
8>> length(C)
9
10ans =
11
12 3
ndims(#)
xxxxxxxxxx
61% ndims(Matrix_name): giving the dimension of the matrix
2>> ndims(C)
3
4ans =
5
6 3
Conversion between Subscripts and indexes
sub2ind
xxxxxxxxxx
71% sub2ind(size,Sub_No): The given subscripts of elements will be converted into homologous indexes.
2>> ind=sub2ind(size(A),[1,2;1,2],[1,1;3,2])
3
4ind =
5
6 1 2
7 5 4
ind2sub
xxxxxxxxxx
131% ind2sub(size,Ind_No): This is the inverse process of the mentioned above.
2>> [I,J]=ind2sub(size(A),ind)
3
4I =
5
6 1 2
7 1 2
8
9
10J =
11
12 1 1
13 3 2
Some Basic Rules
Simple Data Types
Types | Explanations |
---|---|
double | Double-precision floating-point format/64-digit |
char | 16-digit |
sparse | To storage sparse matrix |
unit8 | 8-digit unsigned int |
Logical Functions
Functions | Explanations |
---|---|
iscell(x) | If true, return 1; Else return 0/Cell matrix |
isfield(x) | Ditto |
isfinite(x) | Return the same vector as x |
islogical(x) | Same as /Logical vector |
isnumeric(x) | Ditto/Numeric vector |
isstr(x) | Ditto/String |
isstruct(x) | Ditto/Structure |
isobject(x) | Ditto/Object |
logical(x) | Return a logical vector that can be used |
Predefined Variables
Variables | Explanations |
---|---|
ans | assigned the value of the last expression, which doesn't have a name |
eps | the diff between 1 and the closest representative floating-point number You can set a new value, but it can't restore by the command 'clear'. |
realmax realmin | the biggest/smallest float-point number |
pi | namely |
inf | defined as When 0 becomes the divisor, it will return 'inf' instead of interruption. |
NaN | defined as 'Not a Number' It is '%'type or |
i j | defined as You can set new values for them and restore by the command 'clear'. |
View Variables
commands | Explanations |
---|---|
who | return all the defined variables |
who global | return all the defined global variables |
who a* | return all the defined variables which start with 'a' |
whos whos global | show more details than who /who global |
exist(namestr) | return diff values according to the definitions of variables in 'namestr' |
inmem | return a cell vector, which includes functions and M files in memory so far |
workspace | open 'workspace' |
Delete Variables
commands | Explanations |
---|---|
clear | delete all the defined variables and restore all the predefined except eps |
clear name1 name2 ... | delete the specific variables |
clear a* | delete all the defined variables which start with 'a' |
2 equivalent descriptions:
command variable
command('variable')
Arithmetic Operators
Priority | Operators |
---|---|
H | ^ |
M | * / \ |
L | + - |
Mathematical Functions
Functions | Explanations |
---|---|
abs(x) | absolute value |
sign(x) | sign function |
sqrt(x) | |
pow2(x,f) | |
exp(x) | |
log(x) | |
log10(x) | |
sin(x)... | trigonometric functions |
The x in trigonometric functions must be in radians.
Rounding and Relevant Commands
Commands | Explanations |
---|---|
round(x) | return the integer closest to x |
fix(x) | return the integer closest to x towards 0 |
floor(x) ceil(x) | round down/up |
rem(x,y) | return the remainder of x/y |
gcd(x,y) | GCD |
[a,b,c]=gcd(x,y) | |
lcm(x,y) | LCM |
[t,n]=rat(x) | is the approximate value of x with a relative error less than |
[t,n]=rat(x,tol) | ... less than 'tol' |
rat(x) rat(x,tol) | return continued fraction form of x with a relative error |
Functions on Complex Numbers
Functions | Explanations |
---|---|
real(z) imag(z) | return the real/imaginary part of z |
abs(z) | absolute value |
conj(z) | return the complex conjugate of z |
angle(z) | return the phase angle of z |
unwrap(v) unwrap(v,k) | correct the angle to make the diff of adjacent elements smaller than / |
cplxpair(v) | return complex conjugate pairs |
xxxxxxxxxx
121% e.g. cplxpair
2>> v
3
4v =
5
6 1.0000 + 2.0000i 2.0000 + 3.0000i -1.0000 - 3.0000i 1.0000 - 2.0000i -1.0000 + 3.0000i 2.0000 - 3.0000i 2.0000 + 0.0000i
7
8>> cplxpair(v)
9
10ans =
11
12 -1.0000 - 3.0000i -1.0000 + 3.0000i 1.0000 - 2.0000i 1.0000 + 2.0000i 2.0000 - 3.0000i 2.0000 + 3.0000i 2.0000 + 0.0000i
Coordinate Conversions
Conversions | Explanations |
---|---|
[theta,r]=cart2pol(x,y) | convert Cartesian coordinate system into polar coordinate system |
[x,y]=pol2cart(theta,r) | convert polar coordinate system into Cartesian coordinate system |
[alpha,theta,r]=cart2sph(x,y,z) | convert Cartesian coordinate system into spherical coordinate system |
[x,y,z]=sph2cart(alha,theta,r) | converse process of the above |
Flops and Time Management
flops
Elementary Functions
flops(0)
reset to 0
Time Management
Commands | Explanations |
---|---|
tic | start a timer |
toc | read the time of the timer started by tic |
clock fix(clock) | return the date and time (scientific notation) |
etime(t1,t2) | return the seconds between t1 and t2 |
eputime | return the seconds of CPU since startup of MATLAB |
xxxxxxxxxx
261% e.g.
2>> tic
3>> toc
4历时 2.159932 秒。
5>> clock
6
7ans =
8
9 1.0e+03 *
10
11 2.0210 0.0010 0.0230 0.0170 0.0360 0.0366
12>> fix(clock)
13
14ans =
15
16 2021 1 23 17 35 9
17>> etime([2020 1 23 0 0 0],[2021 1 23 0 0 0])
18
19ans =
20
21 -31622400
22>> cputime
23
24ans =
25
26 111.3125
Commands | Explanations |
---|---|
date | date-month-year |
calendar(yyyy,mm) | return the appointed calendar |
datenum(yyyy,mm,dd) | day: 0000-01-01 |
datestr(d,form) | return the date denoted by form |
datetick(axis,form) | write data on the coordinate axis in the figure |
datevec(d) | return [yyyy mm dd ho mi se] |
enomday(yyyy,mm) | the number of days |
now | similar to datenum |
[daynr dayname]=weekday(day) | return the day of the week (1 is Sunday) |
xxxxxxxxxx
91% one tip
2>> A
3A =
4 1 2
5 3 4
6>> A+100
7ans =
8 101 102
9 103 104
Multiplication
2-dim only
xxxxxxxxxx
291% dot product
2>> x=[1 2 3 4 5];y=[1;2;3;4;5];
3>> x*y
4ans =
5 55
6>> y*x
7ans =
8 1 2 3 4 5
9 2 4 6 8 10
10 3 6 9 12 15
11 4 8 12 16 20
12 5 10 15 20 25
13>> dot(x,y)
14ans =
15 55
16>> dot(y,x)
17ans =
18 55
19>> A=[1 2 3
204 5 6];...
21B=[1 0 0
220 0 1];
23>> dot(A,B) % namely dot(A,B,1)
24ans =
25 1 0 6
26>> dot(A,B,2)
27ans =
28 1
29 6
similar as dot product
Convolution
Tensor Product
Division
inverse matrix: inv(Matrix_name)
Conjugate and Transpose
A.'
or conj(A)
A'
Exponentiation
xxxxxxxxxx
121>> A
2A =
3 1 2
4 2 1
5>> 2^A
6ans =
7 4.2500 3.7500
8 3.7500 4.2500
9>> A^2
10ans =
11 5 4
12 4 5
Element Operations
+
-
.*
./
.\
.^
Matrix Functions
Functions |
---|
expm(Matrix_name) |
logm(Matrix_name) |
sqrtm(Matrix_name) |
Note the difference between expm
and exp
etc.
Relational Operators
<
<=
>
>=
==
~=
The elements of the result matrix are 0(false) or 1(true).
Logical Operators
&
|
~
xor
xxxxxxxxxx
231% e.g.
2>> A=[1 2 0];B=[0 2 3];
3>> A & B
4ans =
5 1×3 logical 数组
6 0 1 0
7>> A | B
8ans =
9 1×3 logical 数组
10 1 1 1
11>> ~ A
12ans =
13 1×3 logical 数组
14 0 0 1
15>> xor(A,B)
16ans =
17 1×3 logical 数组
18 1 0 1
19>> C=1;
20>> A & C
21ans =
22 1×3 logical 数组
23 1 1 0
Logical Functions
find
to give the values and positions of adequate elements of the matrix or vector
xxxxxxxxxx
571% e.g.1 finding nonzero elements
2>> x=[1 2 -1 0 0 3 0 4];
3>> find(x)
4ans =
5 1 2 3 6 8
6>> A=[1 2 0
72 0 0
84 0 3];
9>> find(A)
10ans =
11 1
12 2
13 3
14 4
15 9
16>> [u,v]=find(A)
17u =
18 1
19 2
20 3
21 1
22 3
23v =
24 1
25 1
26 1
27 2
28 3
29>> [u,v,b]=find(A)
30u =
31 1
32 2
33 3
34 1
35 3
36v =
37 1
38 1
39 1
40 2
41 3
42b =
43 1
44 2
45 4
46 2
47 3
48% e.g.2 used with relational operators
49>> find(x>1)
50ans =
51 2 6 8
52>> x(ans)
53ans =
54 2 3 4
55>> length(ans)
56ans =
57 3
any
all
return Boolean values
xxxxxxxxxx
591%
2>> x=[1;0;2];y=[1;1;2];z=[0;0;0];
3>> any([x y z])
4ans =
5 1×3 logical 数组
6 1 1 0
7>> all([x,y,z])
8ans =
9 1×3 logical 数组
10 0 1 0
11>> any(any([x y z]))
12ans =
13 logical
14 1
15>> all(all([x y z]))
16ans =
17 logical
18 0
19% with relational operators
20>> all(y>0)
21ans =
22 logical
23 1
24>> all(all([x y z]>0))
25ans =
26 logical
27 0
28% verify if it is a symmetric matrix
29>> A=[1 2 3
302 1 4
313 4 1]
32A =
33 1 2 3
34 2 1 4
35 3 4 1
36>> all(all(A==A'))
37ans =
38 logical
39 1
40% verify if it is an upper triangular matrix
41>> B=[2 3 4
420 3 2
430 0 1]
44B =
45 2 3 4
46 0 3 2
47 0 0 1
48>> any(any(tril(B,-1)))
49ans =
50 logical
51 0
52>> all(all(B))
53ans =
54 logical
55 0
56>> all(all(B==triu(B)))
57ans =
58 logical
59 1
Some Others
Functions | Explanations |
---|---|
isnan(Matrix_name) | NaN : 1 others: 0 |
isinf(Matrix_name) | inf : 1 others: 0 |
isempty(Matrix_name) | empty: 1 else: 0 |
isequal(Matrix_name1,Matrix_name2) | equal: 1 else: 0 |
isreal(Matrix_name) | real: 1 else: 0 |
isfinite(Matrix_name) | finite: 1 others: 0 |
Basic Matrix Establishment
1-matrix, 0-matrix and Unit Matrix
xxxxxxxxxx
411% ones
2>> ones(3)
3ans =
4 1 1 1
5 1 1 1
6 1 1 1
7>> ones(4,3,2)
8ans(:,:,1) =
9 1 1 1
10 1 1 1
11 1 1 1
12 1 1 1
13ans(:,:,2) =
14 1 1 1
15 1 1 1
16 1 1 1
17 1 1 1
18>> A=[1 2 3 4 5
192 3 1 2 5];
20>> ones(size(A))
21ans =
22 1 1 1 1 1
23 1 1 1 1 1
24% zeros is similar
25% eye
26 % 2-dim only!
27>> eye(4)
28ans =
29 1 0 0 0
30 0 1 0 0
31 0 0 1 0
32 0 0 0 1
33>> eye(3,4)
34ans =
35 1 0 0 0
36 0 1 0 0
37 0 0 1 0
38>> eye(size(A))
39ans =
40 1 0 0 0 0
41 0 1 0 0 0
Random Matrix
rand
to generate random numbers between 0 and 1 evenly
xxxxxxxxxx
201>> rand
2ans =
3 0.4024
4>> rand(4)
5ans =
6 0.6589 0.1084 0.9620 0.2599
7 0.9013 0.0361 0.7461 0.9620
8 0.9954 0.6181 0.6625 0.5402
9 0.6532 0.5671 0.5233 0.0303
10>> rand(4,3,2)
11ans(:,:,1) =
12 0.6963 0.3302 0.2284
13 0.5197 0.2297 0.6520
14 0.0590 0.1139 0.0662
15 0.8900 0.3109 0.2754
16ans(:,:,2) =
17 0.2818 0.6033 0.8486
18 0.8801 0.7833 0.0506
19 0.4443 0.1139 0.4662
20 0.7559 0.9786 0.3257
randn
similar to rand
, normally distributed random number with unit variance and zero mean