1. Real-valued input matrix
One of the powerful features of MATLAB can deal directly reflected in the vector or matrix. Of course, the primary task is the input vector or matrix to be processed.
Whether any matrix (vector), we can directly enter each element row manner: elements in the same row by a comma (,) or separated by a space character, and is not limited to the number of spaces; different row with a semicolon ( ;) delimited. All the elements in one of the brackets ([]) within; when the matrix is multidimensional (three or more), and the element in square brackets is the number of lower-dimensional matrix, there will be multiple brackets. Such as:
>> Time = [11 12 1 2 3 4 5 6 7 8 9 10]
Time =
11 12 1 2 3 4 5 6 7 8 9 10
>> X_Data = [2.32 3.43;4.37 5.98]
X_Data =
2.43 3.43
4.37 5.98
>> vect_a = [1 2 3 4 5]
vect_a =
1 2 3 4 5
>> Matrix_B = [1 2 3;
>> 2 3 4;3 4 5]
Matrix_B = 1 2 3
2 3 4
3 4 5
>> Null_M = []% generates an empty matrix
2. Complex matrix input
There are two ways to generate complex matrix:
The first way
Example 1-1
>> a=2.7;b=13/25;
>> C=[1,2*a+i*b,b*sqrt(a); sin(pi/4),a+5*b,3.5+1]
C=
10000 54000 + 0.8544 05200i
0.7071 5.3000 4.5000
2 ways
Example 1-2
>> R=[1 2 3;4 5 6], M=[11 12 13;14 15 16]
R =
1 2 3
4 5 6
M =
11 12 13
14 15 16
>> CN=R+i*M
CN =
1.0000 +11.0000i 2.0000 +12.0000i 3.0000 +13.0000i
4.0000 +14.0000i 5.0000 +15.0000i 6.0000 +16.0000i
1 .1.2 generated symbol matrix
The method in MATLAB input symbol vector or a matrix and the input vector or matrix of numeric types is very similar in form, but to use a matrix sym-defined function, or a symbol defining the function used syms symbol (Symbol), to define some necessary symbolic variables, then enter the symbol matrix as defined in the general matrix of the same.
1. Sym matrix is defined with the command:
Sym time function is actually a symbolic expression in the definition, when the symbol matrix element may be any symbol or expression, and the length is not limited, but the square brackets to create the symbolic expressions single quotation marks. In the following example:
Example 1-3
>> sym_matrix = sym('[a b c;Jack,Help Me!,NO WAY!],')
sym_matrix =
[a b c]
[Jack Help Me! NO WAY!]
>> sym_digits = sym('[1 2 3;a b c;sin(x)cos(y)tan(z)]')
sym_digits =
[1 2 3]
[a b c]
[Sin (x) cos (y) as (z)]
2. Command syms defined matrix
Define each element of the matrix is variable as a symbol, then the same as an ordinary matrix input symbol matrices.
Example 1-4
>> syms a b c ;
>> M1 = sym('Classical');
>> M2 = sym(' Jazz');
>> M3 = sym('Blues')
>> syms_matrix = [a b c; M1, M2, M3;int2str([2 3 5])]
syms_matrix =
[ a b c]
[Classical Jazz Blues]
[ 2 3 5]
The matrix of values converted to the corresponding symbol matrix.
Numeric and symbolic is not the same in MATLAB, can not be directly converted therebetween. MATLAB provides a numeric converted to the symbolic command, i.e. sym.
Example 1-5
>> Digit_Matrix = [1/3 sqrt(2) 3.4234;exp(0.23) log(29) 23^(-11.23)]
>> Syms_Matrix = sym(Digit_Matrix)
The results are:
Digit_Matrix =
0.3333 1.4142 3.4234
1.2586 3.3673 0.0000
Syms_Matrix =
[ 1/3, sqrt(2), 17117/5000]
[5668230535726899*2^(-52),7582476122586655*2^(-51),5174709270083729*2^(-103)]
Note: the matrix is expressed as a fraction or in the form of floating-point format, is converted into a symbol matrix array, we will have the form closest to the original value representations or rational function is expressed in the form.
1.1.3 generate large matrices
For large matrix, generally create M-files, in order to modify:
Example 1-6 to create a large matrix with M file named example.m
exm = [456 468 873 2579 55
21 687 54 488 8 13
65 4567 88 98 21 5
456 68 4589 654 5 987
5488 10 9 6 33 77]
In the MATLAB window type:
>>example;
>> size (exm)% of display size exm
years =
56% indicated exm 5 rows and 6 columns.
1.1. 4 to create a multidimensional array
Functions cat
Format A = cat (n, A1, A2, ..., Am)
DESCRIPTION n = 1 and n = 2 are respectively configured [A1; A2] and [A1, A2], are two-dimensional arrays may be constructed and n = 3-dimensional array.
Example 1-7
>> A1=[1,2,3;4,5,6;7,8,9];A2=A1';A3=A1-A2;
>> A4=cat(3,A1,A2,A3)
A4 (:,:, 1) =
1 2 3
4 5 6
7 8 9
A4 (:,:, 2) =
1 4 7
2 5 8
3 6 9
A4 (:,:, 3) =
0 -2 -4
2 0 -2
4 2 0
Or the original may be defined in another way:
Example 1-8
>> A1=[1,2,3;4,5,6;7,8,9];A2=A1';A3=A1-A2;
>> A5 (:,:, 1) = A1, A5 (:,:, 2) = A2, A5 (:,:, 3) = A3
A5 (:,:, 1) =
1 2 3
4 5 6
7 8 9
A5 (:,:, 2) =
1 4 7
2 5 8
3 6 9
A5 (:,:, 3) =
0 -2 -4
2 0 -2
4 2 0
1.1. 5 generates special matrix
Order all-zero matrix
Function zeros
Format B = zeros (n)% generates an all-zero n × n matrix
B = zeros (m, n)% m × n generates an all-zero matrix
B = zeros ([mn])% m × n generates an all-zero matrix
B = zeros (d1, d2, d3 ...)% generated d1 × d2 × d3 × ... all zeros matrix or array
B = zeros ([d1 d2 d3 ...])% generated d1 × d2 × d3 × ... all zeros matrix or array
% A generates the same size as the matrix B = zeros (size (A)) all-zero matrix
Command unit matrix
Functions eye
Form Y = eye (n)% n × n unit matrix generated
Y = eye (m, n)% generates m × n unit matrix
% To generate the same size as the matrix A Y = eye (size (A)) unit matrix
Command the whole array 1
Function ones
Form Y = ones (n)% 1 generates a full n × n matrix
Y = ones (m, n)% 1 generates a full array of m × n
Y = ones ([mn])% 1 generates a full array of m × n
Y = ones (d1, d2, d3 ...)% generated d1 × d2 × d3 × ... a whole array or an array
Y = ones ([d1 d2 d3 ...])% generated d1 × d2 × d3 × ... a whole array or an array
% To generate the same size as the matrix A Y = ones (size (A)) a whole array
Command uniformly distributed random matrix
Function rand
Form Y = rand (n)% randomly generates n × n matrix, in which element (0,1) of
Y = rand (m, n)% randomly generates m × n matrix
Y = rand ([mn])% randomly generates m × n matrix
Y = rand (m, n, p, ...)% generates m × n × p × ... random matrix or array
Y = rand ([mnp ...])% generates m × n × p × ... random matrix or array
Y = rand (size (A)) to generate a random matrix of the same size as% matrix A
rand% a random number is generated only when there is no input variables
s = rand ( 'state')% uniformity generating a current state vector generator comprising elements 35
rand ( 'state', s)% reset to a state s
rand ( 'state', 0)% reset to the initial state generator
rand ( 'state', j)% reset generator of integer j to the j-th state
rand ( 'state', sum (100 * clock))% each reset to a different state
Example 1-9 generates a random matrix 3 × 4
>> R=rand(3,4)
R =
0.9501 0.4860 0.4565 0.4447
0.2311 0.8913 0.0185 0.6154
0.6068 0.7621 0.8214 0.7919
Example 1-10 generates an interval [10, 20] of order 4 in the uniformly distributed random matrix
>> a=10;b=20;
>> x=a+(b-a)*rand(4)
x =
19.2181 19.3547 10.5789 11.3889
17.3821 19.1690 13.5287 12.0277
11.7627 14.1027 18.1317 11.9872
14.0571 18.9365 10.0986 16.0379
Command normally distributed random matrix
Function randn
Form Y = randn (n)% normal random generating n × n matrix
Y = randn (m, n)% normal random generated m × n matrix
Y = randn ([mn])% normal random generated m × n matrix
Y = randn (m, n, p, ...)% generates m × n × p × ... normally distributed random matrix or array
Y = randn ([mnp ...])% generates m × n × p × ... normally distributed random matrix or array
% Normal random matrix produces the same size matrix A Y = randn (size (A))
Generating a normal random number only when no input variables randn%
s = randn ( 'state')% generates normal vector generator comprising a current state of two elements
s = randn ( 'state', s)% s reset state
s = randn ( 'state', 0)% to the initial reset state generator
s = randn ( 'state', j)% for integer j j reset state to the second state
s = randn ( 'state', sum (100 * clock))% each reset to a different state
Example 1-11 mean of 0.6, 0.1 variance matrix of order 4
>> mu=0.6; sigma=0.1;
>> x=mu+sqrt(sigma)*randn(4)
x =
0.8311 0.7799 0.1335 1.0565
0.7827 0.5192 0.5260 0.4890
0.6127 0.4806 0.6375 0.7971
0.8141 0.5064 0.6996 0.8527
Command generating randomly arranged
Function randperm
Format p = randperm (n)% random arrangement generates an integer between 1 ~ n
Example 1-12
>> randperm(6)
years =
3 2 1 5 4 6
Aliquots command generation linear vector
Function linspace
Form y = linspace (a, b)% 100 generates linear aliquots points (a, b) on
y = linspace (a, b, n)% n linearly generated on the dividing points (a, b)
Command generation number of aliquots vector
Function logspace
Form y = logspace (a, b) % in ( yields 50 aliquots vector between log)
y = logspace(a,b,n)
y = logspace (a, pi)
Command calculating the number of elements in the matrix
The number n = numel (a)% returns matrix elements A
Command generation element is input to the diagonal element of the matrix
Function blkdiag
Format out = blkdiag (a, b, c, d, ...)% is generated in a, b, c, d, ... as the diagonal elements of the matrix
Example 1-13
>> out = blkdiag(1,2,3,4)
out =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
Friends command matrix
Function compan
Format A = compan (u)% u polynomial vector system, A is a companion matrix, the first row of the element A -u (2: n) / u (1), wherein u (2: n) is the first u 2 to the n-th element, a is a characteristic value is the characteristic polynomial roots.
Examples 1-14 polynomial companion matrices and root
>> u=[1 0 -7 6];
Companion matrix >> A = compan (u)% of the polynomial
A =
0 7 -6
1 0 0
0 1 0
Wherein >> eig (A)% A value is the root of the polynomial
years =
-3.0000
2.0000
1.0000
Matrix command hadamard
Function hadamard
Form H = hadamard (n)% returns matrix of order n hadamard
Example 1-15
>> h=hadamard(4)
h =
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Hankel matrix command
Function hankel
Form H = hankel (c)
The first column% 1 element is c, inverse trigonometric following elements to zero. H = hankel (c, r)% is the first column of elements c 1, the last element is r, if the last element of the first element and the c different r, the position of the cross element is taken as the last element of c.
Example 1-16
>> c=1:3,r=7:10
c =
1 2 3
r =
7 8 9 10
>> h=hankel(c,r)
h =
1 2 3 8
2 3 8 9
3 8 9 10
Hilbert matrix command
Function hilb
Form H = hilb (n)% Returns the n-order Hilbert matrix whose elements H (i, j) = 1 / (i + j-1).
Example 1-17 generates a third-order Hilbert matrix
>> format rat% output in the form of rational
>> H=hilb(3)
H =
1 1/2 1/3
1/2 1/3 1/4
1/3 1/4 1/5
Hilbert matrix inverse order
Function invhilb
Form H = invhilb (n)% Inverse Hilbert matrix of order n is generated
Command Magic (Cube) matrix
Function magic
Form M = magic (n)% is generated cube matrix of order n
Examples 1-18
>> M=magic(3)
M =
8 1 6
3 5 7
4 9 2
Pascal matrix command
Function pascal
Format A = pascal (n)% Pascal matrix of order n is generated, it is a symmetric, positive definite matrix, its elements composed of the Pascal triangle, all elements of its inverse matrix are integers.
A = pascal (n, 1)% returns matrix of Pascal lower triangular Cholesky factor consisting of
A = pascal (n, 2)% Returns Pascal (n, 1) in the form of exchange and transposition
Examples 1-19
>> A=pascal(4)
A =
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20
>> A=pascal(3,1)
A =
1 0 0
1 -1 0
1 -2 1
>> A=pascal(3,2)
A =
1 1 1
-2 -1 0
1 0 0
Command Toeplitz matrix
Function toeplitz
Format T = toeplitz (c, r)% generates an asymmetric Toeplitz matrix, c as the first column, the first row as r, the remaining elements of the upper left corner and adjacent elements are equal.
T = toeplitz (r)% generates a symmetric Toeplitz matrix by a vector r
Examples 1-20
>> c=[1 2 3 4 5];
>> r=[1.5 2.5 3.5 4.5 5.5];
>> T=toeplitz(c,r)
T =
1 5/2 7/2 9/2 11/2
2 1 5/2 7/2 9/2
3 2 1 5/2 7/2
4 3 2 1 5/2
5 4 3 2 1
Wilkinson command value Test Array
Function wilkinson
Form W = wilkinson (n)% n order Wilkinson return value Test Array
Examples 1-21
>> W=wilkinson(4)
W =
3/2 1 0 0
1 1/2 1 0
0 1 1/2 1
0 0 1 3/2
>> W=wilkinson(7)
W =
3 1 0 0 0 0 0
1 2 1 0 0 0 0
0 1 1 1 0 0 0
0 0 1 0 1 0 0
0 0 0 1 1 1 0
0 0 0 0 1 2 1
0 0 0 0 0 1 3