Statistical functions
SUM (A) sum
If a is a vector, sum (a) calculates each element's sum
If A is a matrix, sum (a) evaluates the sum of each column element (that is, each column is treated as a vector, and a row vector is returned, resulting in the sum of the elements of each column)
If A is a multidimensional array, sum (A,dim) sums the corresponding position of the specified dimension, that is, the column is summed when Dim=1, and the column is summed at dim=2 time, and so on.
b = SUM (..., ' double ') and B = sum (..., dim, ' double ') This is used to specify the calculation precision, forcing the return type to double
b = SUM (..., ' native ') and B = sum (..., dim, ' native ') This is used to specify the calculation precision, forcing the return type precision to be the same as the right
If the precision ' double ' or ' Natvie ' is not specified above, if a is an integer type, the double is returned by default, if a is a single or double type, the default returns single or double precision
Example 1+2+...+100
Clear
CLC
a=1:100;
SUM (A)
Results
Ans =
5050
Cases
Clear
CLC
a=magic (3)
sum (a)
sum (sum (a))
Results
A =
8 1 6
3 5 7
4 9 2
ans =
all
ans =
45
Cases
Clear
CLC
a=magic (4)
sum (a,1)
sum (a,2)
Results
A = 2 3
5 8
9 7 6 (4) 15 1 ans = over-the-up
ans
=
34
Example multidimensional vector
Clear
CLC
A (:,:, 1) =[1 0 2 5;4 1 8 7;3 2 6 3];
A (:,:, 2) =[3 5 4 1;2 6 2 1;4 2 3 0];
A
sum (a,1)
sum (a,2)
sum (a,3)
Results
A (:,:, 2) =
3 5 4 1
2 6 2 1
4 2 3 0
ans (:,:, 1) =
8 3
ans (:,:, 2) =
9 9 2
ans (:,:, 1) =
8
20
ans (:,:, 2) =
9
ans =
4 5 6 6 6 7 10 8
7 4) 9 3
Example accuracy
Clear
CLC
a=int8 (1:20)
b=sum (a)%a is an integer type, when precision is not specified, the default returns double precision, the result
is correct c=sum (a, ' native ')% because A is a 8-bit integer type, The precision is set to native, so the result precision is int8 (8-bit integer type), the integer range of int8 is -128~127, the range is truncated, so the result is 127
class (b)% view B data type
class (C)% View the data type of C
Results
A =
Columns 1 through
1 2 3 4 5 6 7 8 9 ten 12 Columns- through -
B =
210
C =
127
ans =
double
ans =
int8
Mean (A) averages, parameters are similar to sum, but mean cannot specify precision
If a is a vector, mean (a) calculates the average of all elements of a
If A is a matrix, mean (a) calculates the average of each column element (that is, each column is treated as a vector, and a row vector is returned, resulting in the average of each column element)
If A is a multidimensional array, SUM (A,dim) averages the corresponding position of the specified dimension, that is, the average value of the column at Dim=1, the average of the column at dim=2, and the second analogy
Cases
Clear
CLC
% averages a=1:100 for vectors
;
Mean (A)
% of the matrix averaging
b=magic (4)
mean (B)
% to the multidimensional vector averaging
C (:,:, 1) =[3 5 8 6;3 7 9 4;3 4);
C (:,:, 2) =[2 7 4 3;6 3 4;10 7 9 4];
C
mean (c,1)
mean (c,2)
mean (c,3)
Results
Ans =
8.5000 8.5000 8.5000 8.5000
C (:,:, 1) =
3 5 8 6
3 7 9 4
3 4
C (:,:, 2) =
2 7 4 3
6 3 10 4 7 9 4
ans (:,:, 1) =
3.0000 5.3333 9.6667 8.0000
ans (:,:, 2) =
6.0000 5.6667 7.6667 3.6667
ans (:,:, 1) =
5.5000
5.7500
8.2500
ans (:,:, 2) =
4.0000
5.7500
7.5000
ans =
2.5000 6.0000 6.0000 4.5000
4.5000 5.0000 9.5000 4.0000
6.5000 5.5000 10.5000 9.0000
Median the median min to find the minimum max to find the maximum value
Prod the product sort sort
The parameters and methods used are basically consistent with the mean averaging.
Sort is sorted by default in ascending order, but it can also be specified in ascending or descending order by parameter
Sort (x,dim,mode), DIM does not specify the default is 1,mode when the value is ' ascend ', in ascending order (default), MODE value is ' descend ' when sorted in descending order
Cases
Clear
CLC
a=[11 6 9 1 1 8 2 9];
Sort (A, ' ascend ')
sort (A, ' descend ')
Results
Ans =
1 1 2 6 8 9 9 each
ans = 11 9 9 8 6 2 1 1
Another approach to descending is to reverse the output
Clear
CLC
a=[11 6 9 1 1 8 2 9];
B=sort (A)
c=b (end:-1:1)
Results
B =
1 1 2 6 8 9 9 each
C = 9 9 8 6 2 1 1
Deviations and correlations
var (x) to find the variance of X
STD (x) to find the standard deviation of X
Range (x) to find the extreme difference of X
CoV (x) to find the covariance matrix of X
CoV (x, y) to find the covariance of X, y two matrices
CORRCOEF (x) to find the autocorrelation matrix of X
Corrcoef (x, y) to find the cross-correlation coefficients of × and y, the result is square
CORR2 (x, y) to find correlation coefficients
Cases
Clear
CLC
a=randn (5,5)% generates a 5x5 standard normal distribution (mean 0, Variance 1) of the random number matrix
b=var (a)% for the variance of a
B2=var (a (:))% of the variance of all numbers
c=std (a)% seeking a standard deviation
C2=STD (A (:))% for the standard deviation of all numbers
d=range (a)% for the difference of a
e=cov (a)% for a covariance matrix
f=corrcoef (a)% for A's autocorrelation array
Results
A = 1.6035-0.1559-1.2507 0.0125 0.9337 1.2347 0.2761-0.9480-3.0292 0.3503-0.2296-0 .2612-0.7411-0.4570-0.0290-1.5062 0.4434-0.5078 1.2424 0.1825-0.4446 0.3919-0.3206-1 .0667-1.5651 B = 1.6320 0.1056 0.1332 2.4728 0.8687 B2 = 1.0286 C = 1.2775 0.3250 0.3650 1.5725 0.9320 C2 = 1.0142 D = 3.1096 0.7046 0.9301 4.2716 2.4988 E = 1.632 0-0.1957-0.4006-1.1446 0.5936-0.1957 0.1056 0.0748-0.0320-0.1390-0.4006 0.0748 0.133 2 0.1051-0.2914-1.1446-0.0320 0.1051 2.4728 0.1939 0.5936-0.1390-0.2914 0.1939 0.868 7 F = 1.0000-0.4713-0.8592-0.5698 0.4985-0.4713 1.0000 0.6307-0.0626-0.4589-0.8592 0.6307 1.0000 0.1830-0.8564-0.5698-0.0626 0.1830 1.0000 0.1323 0.4985-0.4589-0.8564
0.1323 1.0000
Polynomial calculation
Defining the polynomial
P=[A1 A2 ... an an+1]
Y = P (1) *x^n + P (2) *x^ (N-1) + ... + p (N) *x + P (n+1)
That
Y = a1*x^n + a2*x^ (N-1) + ... + an*x + an+1
Poly to find the characteristic polynomial
Poly (a) when a is a n*n matrix, the poly (a) command evaluates the characteristic polynomial of a, Det (Lambda*eye (Size (a))-a)
Poly (v) when V is a vector, command poly (v) generates a polynomial with V as its root
Root to find the roots of the polynomial
Root (P)
Cases
Clear
CLC
a=[1 2 3;4 5 6;7 8 0];
P=poly (A)% to find the characteristic polynomial |λe-a|
R=roots (p)%, based on the above characteristic polynomial, to find eigenvalues
Results
p =
1.0000 -6.0000 -72.0000 -27.0000
r =
12.1229
-5.7345
-0.3884
Hand calculation process
Cases
Clear
CLC
a=[1 2 3];
P=poly (A)
Results
p =
1 -6 -6
Hand calculation process
Help document the algorithm for finding the characteristic polynomial |λe-a|
n = Length (a)
z = Eig (a);% evaluates all eigenvalues of matrix A, forming vector z
c = zeros (n+1,1); C (1) = 1;
For j = 1:n
C (2:j+1) = C (2:j+1)-Z (j) *c (1:J);
End
Polyval Polynomial evaluation
Polyval (p,x) P is a polynomial, X can be a vector or a matrix, when a vector, the result is a vector, the MXN matrix, each element is evaluated, the result is still the MXN matrix
Example p (x) = 3x^2+2x+1 at x = 5,7, and 9:
Clear
CLC
p=[3 2 1];
Polyval (p,[5 7 9])
Results
Ans = 162 262
Example p (x) = 4x^2+4x+1 at x = [2 3 4;5 6 7]:
Clear
CLC
p=[4 4 1];
Polyval (p,[2 3 4;5 6 7])
Results
Ans =
225 bayi 121 169
Conv polynomial multiplication
Conv (p1,p2) polynomial-P1 and polynomial P2 multiplication
Deconv Polynomial Division
[Q,r]=deconv (P1,P2) polynomial P1 and polynomial P2 divide, Q is quotient, R is the remainder
The derivation of Polyder polynomial
Polyder (p)-derivative of the polynomial P
Polyder (P1,P2) polynomial P1 and polynomial P2 multiply, equivalent to Polyder (CONV (P1,P2))
[Q,d]=polyder (P1,P2) polynomial fraction p1/p2 (i.e., P1 is a molecule, P2 is the denominator), the result is still fractional q/d (that is, Q is a molecule, D is the denominator)
Example U=X^3+2X^2+3X+4,V=10X^2+20X+30, seeking c=u*v,c/u
Clear
CLC
u=[1 2 3 4];
V=[10];
C=conv (u,v)
[Q,r]=deconv (C,u)
Results
c =
Ten +
q =
ten +
r =
0 0 0 0 0 0
Example a=3x^2+6x+9,b=x^2+2x, finding the derivative of a*b
Clear
CLC
a=[3 6 9];
B=[1 2 0];
K=polyder (A, B)
Results
K = 18
function Extremum and 0 points
Fminbnd Seeking Extremum
X = Fminbnd (fun,x1,x2) to function value fun for minimum value, x1,x2 for interval, x1 < X < X2
If a maximum value is required, the derivative can be taken first
Cases
Clear
CLC
X1 = fminbnd (@cos, 3,4)
X2 = Fminbnd (@ (x) sin (x) +3,2,5)
f = @ (X,c) (X-C). ^2;
c = 1.5;
X3 = Fminbnd (@ (x) f (x,c), 0, 1)
Results
X1 =
3.1416
X2 =
4.7124
X3 =
0.9999
Fzero 0 Points
X = Fzero (fun,x0) 0 points for function fun, X0 is the initial value, fun function can use @
Cases
Clear
CLC
X1 = Fzero (@sin, 3) The % function is sin x, the initial value is 3
X2 = Fzero (@ (x) sin (3*x), 2) The function is sin 3x, the initial value is 2
f = @ (x , c) cos (c.*x); % defines a function with parameters
c = 2; The value of the% setting parameter
X3 = Fzero (@ (x) f (x,c), 0.1)
X4 = Fzero (' x.^3-2.^x+1 ', -1) the % function is x^3-2^x+1, the initial value is-1
Results
X1 =
3.1416
X2 =
2.0944
X3 =
0.7854
X4 =
-0.7368