§1 Basic ConceptsSome ShortcutsSome TipsNumerical MatricesVariablesArithmetic Expressions and Mathematical Functions§2 Matrix OperationsBasic OperationsMatrix Functions and OperatorsMatrix Creation
xxxxxxxxxx71exit,quit % quit2Ctrl-C % interruption3Ctrl-l % moving one word to the left4Ctrl-r % .......................right5Ctrl-a % moving to the beginning of the line6Ctrl-e % ..............end..................7Ctrl-k % deleting from here to the end of the lineYou 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
xxxxxxxxxx581>> format2>> p=1/3-134p =56 -0.666778>> format short;p910p =1112 -0.66671314>> format long;p1516p =1718 -0.6666666666666671920>> format short e;p2122p =2324 -6.6667e-012526>> format long e;p2728p =2930 -6.666666666666667e-013132>> format hex;p3334p =3536 bfe55555555555563738>> format +;p3940p =4142-4344>> format bank;p4546p =4748 -0.674950>> format rat;p5152p =5354 -2/355 56>> format compact;p57p =58 -2/3Help Commands
2-dim Matrices
row: separated by ' ' or ','
column: separated by ';' or Enter
xxxxxxxxxx101% e.g.2>> A=[1,2,3;4,5,6;]3% or4>> A=[1 2 354 5 6]67A =89 1 2 310 4 5 6xxxxxxxxxx101% 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 ) = 067B =89 1 710 -5 0Multi-dim Matrices
xxxxxxxxxx311% e.g.1 constructed from 2 known 2-dim matrices2>> A=[1 2 334 5 6];4>> B=[11 12 13514 15 16];6>> C(:,:,1)=A;7>> C(:,:,2)=B89C(:,:,1) =1011 1 2 312 4 5 6131415C(:,:,2) =1617 11 12 1318 14 15 1619% e.g.2 change one element of the above C20>> C(1,1,1)=1002122C(:,:,1) =2324 100 2 325 4 5 6262728C(:,:,2) =2930 11 12 1331 14 15 16Dimensions and Lengths
size(#)
xxxxxxxxxx241% size(Matrix_name): giving the length of every dim2>> size(A)34ans =56 2 37% size(Matrix_name,Dim_number): giving the length of the specific dim8>> size(A,2)910ans =1112 313% size(Row_vector_name/Column_vector_name):returning 1 & the length of the row vector / the length of the column vector & 114>> X=[1 2 3 4 5]1516X =1718 1 2 3 4 51920>> size(X)2122ans =2324 1 5length(#)
xxxxxxxxxx121% length(Vector_name):giving the length of the vector2>> length(X)34ans =56 57% length(Matrix_name):giving the maximum of the length of each dimension of the matrix8>> length(C)910ans =1112 3ndims(#)
xxxxxxxxxx61% ndims(Matrix_name): giving the dimension of the matrix2>> ndims(C)34ans =56 3Conversion between Subscripts and indexes
sub2ind
xxxxxxxxxx71% 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])34ind =56 1 27 5 4ind2sub
xxxxxxxxxx131% ind2sub(size,Ind_No): This is the inverse process of the mentioned above.2>> [I,J]=ind2sub(size(A),ind)34I =56 1 27 1 28910J =1112 1 113 3 2Some 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 |
xxxxxxxxxx121% e.g. cplxpair2>> v34v =56 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.0000i78>> cplxpair(v)910ans =1112 -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.0000iCoordinate 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 |
clockfix(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 |
xxxxxxxxxx261% e.g.2>> tic3>> toc4历时 2.159932 秒。5>> clock67ans =89 1.0e+03 *1011 2.0210 0.0010 0.0230 0.0170 0.0360 0.036612>> fix(clock)1314ans =1516 2021 1 23 17 35 917>> etime([2020 1 23 0 0 0],[2021 1 23 0 0 0])1819ans =2021 -3162240022>> cputime2324ans =2526 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) |
xxxxxxxxxx91% one tip2>> A3A =4 1 25 3 46>> A+1007ans =8 101 1029 103 104Multiplication
2-dim only
xxxxxxxxxx291% dot product2>> x=[1 2 3 4 5];y=[1;2;3;4;5];3>> x*y4ans =5 556>> y*x7ans =8 1 2 3 4 59 2 4 6 8 1010 3 6 9 12 1511 4 8 12 16 2012 5 10 15 20 2513>> dot(x,y)14ans =15 5516>> dot(y,x)17ans =18 5519>> A=[1 2 3204 5 6];...21B=[1 0 0220 0 1];23>> dot(A,B) % namely dot(A,B,1)24ans =25 1 0 626>> dot(A,B,2)27ans =28 129 6similar as dot product
Convolution
Tensor Product
Division
inverse matrix: inv(Matrix_name)
Conjugate and Transpose
A.' or conj(A)A' Exponentiation
xxxxxxxxxx121>> A2A =3 1 24 2 15>> 2^A6ans =7 4.2500 3.75008 3.7500 4.25009>> A^210ans =11 5 412 4 5Element 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
xxxxxxxxxx231% e.g.2>> A=[1 2 0];B=[0 2 3];3>> A & B4ans =5 1×3 logical 数组6 0 1 07>> A | B8ans =9 1×3 logical 数组10 1 1 111>> ~ A12ans =13 1×3 logical 数组14 0 0 115>> xor(A,B)16ans =17 1×3 logical 数组18 1 0 119>> C=1;20>> A & C21ans =22 1×3 logical 数组23 1 1 0Logical Functions
find to give the values and positions of adequate elements of the matrix or vector
xxxxxxxxxx571% e.g.1 finding nonzero elements2>> x=[1 2 -1 0 0 3 0 4];3>> find(x)4ans =5 1 2 3 6 86>> A=[1 2 072 0 084 0 3];9>> find(A)10ans =11 112 213 314 415 916>> [u,v]=find(A)17u =18 119 220 321 122 323v =24 125 126 127 228 329>> [u,v,b]=find(A)30u =31 132 233 334 135 336v =37 138 139 140 241 342b =43 144 245 446 247 348% e.g.2 used with relational operators49>> find(x>1)50ans =51 2 6 852>> x(ans)53ans =54 2 3 455>> length(ans)56ans =57 3any all return Boolean values
xxxxxxxxxx591%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 07>> all([x,y,z])8ans =9 1×3 logical 数组10 0 1 011>> any(any([x y z]))12ans =13 logical14 115>> all(all([x y z]))16ans =17 logical18 019% with relational operators20>> all(y>0)21ans =22 logical23 124>> all(all([x y z]>0))25ans =26 logical27 028% verify if it is a symmetric matrix29>> A=[1 2 3302 1 4313 4 1]32A =33 1 2 334 2 1 435 3 4 136>> all(all(A==A'))37ans =38 logical39 140% verify if it is an upper triangular matrix41>> B=[2 3 4420 3 2430 0 1]44B =45 2 3 446 0 3 247 0 0 148>> any(any(tril(B,-1)))49ans =50 logical51 052>> all(all(B))53ans =54 logical55 056>> all(all(B==triu(B)))57ans =58 logical59 1Some 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
xxxxxxxxxx411% ones2>> ones(3)3ans =4 1 1 15 1 1 16 1 1 17>> ones(4,3,2)8ans(:,:,1) =9 1 1 110 1 1 111 1 1 112 1 1 113ans(:,:,2) =14 1 1 115 1 1 116 1 1 117 1 1 118>> A=[1 2 3 4 5192 3 1 2 5];20>> ones(size(A))21ans =22 1 1 1 1 123 1 1 1 1 124% zeros is similar25% eye26 % 2-dim only!27>> eye(4)28ans =29 1 0 0 030 0 1 0 031 0 0 1 032 0 0 0 133>> eye(3,4)34ans =35 1 0 0 036 0 1 0 037 0 0 1 038>> eye(size(A))39ans =40 1 0 0 0 041 0 1 0 0 0Random Matrix
rand to generate random numbers between 0 and 1 evenly
xxxxxxxxxx201>> rand2ans =3 0.40244>> rand(4)5ans =6 0.6589 0.1084 0.9620 0.25997 0.9013 0.0361 0.7461 0.96208 0.9954 0.6181 0.6625 0.54029 0.6532 0.5671 0.5233 0.030310>> rand(4,3,2)11ans(:,:,1) =12 0.6963 0.3302 0.228413 0.5197 0.2297 0.652014 0.0590 0.1139 0.066215 0.8900 0.3109 0.275416ans(:,:,2) =17 0.2818 0.6033 0.848618 0.8801 0.7833 0.050619 0.4443 0.1139 0.466220 0.7559 0.9786 0.3257randn similar to rand, normally distributed random number with unit variance and zero mean