Basic operation of Matlab vector and matrix

Source: Internet
Author: User

Creation of matrices
(1) Rand (M,N) creates a random matrix of M row n columns (the value of each element is between 0 and 1).
(2) Zeros (m,n) creates a 0 matrix of M row n columns.
(3) Ones (M,n) creates a 1 matrix of M row n columns
(4) Eye (m) creates the diagonal element of M Row m column is 1, the remaining element is the diagonal matrix of 0.
(5)

RANDN: A function of a random number or matrix that produces a normal distribution

RANDN: Generates a mean value of 0, the variance σ^2 = 1, the standard deviation σ = 1 of the normal distribution of the random number or matrix function.

Usage:

Y = RANDN (n): Returns the matrix of a n*n random item. If n is not a quantity, an error message is returned. 

y = Randn (m,n) or y = randn ([M n]): Returns a random item matrix of M*n.

y = Randn (m,n,p,...) or y = randn ([m n p ...]) : Generates a random array.

Y = Randn (Size (A)): Returns a random array with the same dimension size as a.

Note: due to the random number sequence, the mean value here is 0, only that the write random number of the distribution expected to be 0, instead of saying that the average of this sequence must be 0.

The normal distribution is manifested when the number of builds is large:

X=RANDN (10000000,1);

hist (x,1000);

(6) x = Diag (v,k) takes the element of Vector v as the nth diagonal element of the matrix X, when k=0, V is the main diagonal of X, and when K>0, V is the upper section K diagonal line, and when K<0, V is the diagonal of the K bar below.

Getting the elements of a matrix
(1) A (x, y) indicates the value of the element in line x, column Y
(2) A (X0:Y0,X1:Y1) represents the value of all elements of line x0 to the y0 row and the X1 column to the Y1 column, and the return value is a sub-matrix of the original matrix.
(3) A (x:y) indicates the value of the first column of the row x to the Y-line element, and the return value is a row vector.
(4) A (x) indicates the value of the first column of row X, and the return value is an element.
Functions of matrices
(1) Find (a conditional expression) A is a matrix or vector, and the return value is the subscript value of the vector that spliced the end of each column with the head of the next column

Example:

x =

0.4218 0.7431 0.6948 0.4456 0.9597 0.5472 0.1966 0.7572

0.9157 0.3922 0.3171 0.6463 0.3404 0.1386 0.2511 0.7537

0.7922 0.6555 0.9502 0.7094 0.5853 0.1493 0.6160 0.3804

0.9595 0.1712 0.0344 0.7547 0.2238 0.2575 0.4733 0.5678

0.6557 0.7060 0.4387 0.2760 0.7513 0.8407 0.3517 0.0759

0.0357 0.0318 0.3816 0.6797 0.2551 0.2543 0.8308 0.0540

0.8491 0.2769 0.7655 0.6551 0.5060 0.8143 0.5853 0.5308

0.9340 0.0462 0.7952 0.1626 0.6991 0.2435 0.5497 0.7792

0.6787 0.0971 0.1869 0.1190 0.8909 0.9293 0.9172 0.9340

0.7577 0.8235 0.4898 0.4984 0.9593 0.3500 0.2858 0.1299

>> Find (x>0.9)

Ans =

2

4

8

23

41

50

59

69

79

(2) Pow2 (a) A is a matrix or a vector

A=

X1 X2

X3 x4

Then Pow2 (A) =

2^x1 2^x2

2^x3 2^x4

(3) A*a A is an element, A is a matrix

A=

X1 X2

X3 x4

A*a=

A*x1 a*x2

A*x3 a*x4

(4) A is a m*n matrix and B is a m*n matrix.

A*b is the multiplication of matrices, and a.*b is the point multiplication of matrices.

Example:

x =

1 2
3 4

>> y=x

y =

1 2
3 4

>> X*y

Ans =

7 10
15 22

>> X.*y

Ans =

1 4
9 16

(5) poly (vector x), poly is the coefficient of the polynomial in which vector x is followed.

Example:

Poly ([up])

Ans =

1-3 2

Get the equation of f (x) =x^2-3^x+2, 1 and 2 are the root

(6)

POLY2STR (x, ' argument name ') or POLY2STR (x) x is a vector

Poly2sym (x, ' argument name ') or POLY2STR (x) x as vector% can only be used when the poly2sym is actually calculated

Example

>> poly2str ([1,2,3,4,5], ' x ')

Ans =

X^4 + 2 X^3 + 3 x^2 + 4 x + 5

Example

>> T=poly2sym ([1,2,3,4], ' x ')

t =

X^3 + 2*x^2 + 3*x + 4

>> Subs (t,1)

Ans =

10

(7) Subs (function, {corresponding variable},{substitution value})% substituting specific value into function

>> f=x+y^2+z^3

f =

y^2 + z^3 + x

>> Subs (F,[x,y,z],[z,y,x])

Ans =

X^3 + y^2 + Z

>> Subs (f,[x,y,z],[1,2,3])

Ans =

32

(8) diff (function name, variable name, number of derivative)

>> syms x y z
>> f=sin (x) +cos (y) +z

f =

z + cos (y) + sin (x)

>> diff (f,x,1)

Ans =

COS (x)

>> diff (f,y,1)

Ans =

-sin (y)

>> diff (f,z,1)

Ans =

1

>> diff (f,x,2)

Ans =

-sin (x)

(7) Points

Indefinite integral: Int (function, argument)

Definite integral: int (function, argument, start address, end address)

Indefinite integral:

>> F=x^2+y

f =

X^2 + y

>> Int (f,x)

Ans =

X^3/3 + y*x

>> Int (f,y)

Ans =

(y* (2*x^2 + y))/2

>>

Definite integral:

>> f=x

f =

X

>> Int (f,x,0,2)

Ans =

2

(10) Rotation, transpose, flip matrix

Flipud (A): Flip matrix up and down

FLIPLR (A): Left and right flip matrix

A ': Transpose matrix

Rot90 (A): Rotate the matrix 90 degrees counterclockwise

A =

1 2 3
4 5 6

>> A '

Ans =

1 4
2 5
6 S

>> Rot90 (A)

Ans =

6 S
2 5
1 4

>> FLIPLR (A)

Ans =

3 2 1
6 5 4

>> Flipud (A)

Ans =

4 5 6
1 2 3

Basic operation of Matlab vector and matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.