Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MatLab study notes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

MatLab (Matrix Laboratory), an interactive programming language based on matrix computing, is designed to meet the needs of science, engineering computing and drawing. There are many functions.

Common command

Esc restores the blank state entered by the command

Arrow keys: call up the previous line and the next line of command

Exit/quit exits and closes Matlab

Figure/figure (2)

Logo displays the Logo of MatLab

Clc screen cleaning

Help sqrt

Demo

Formatcompact

Formatloose

Cleari, j

Clearall

Who or whos, check variables and their degrees in the workspace

You can use a carriage return or comma at the end of the statement, and the result of the operation is immediately displayed. If you do not want the results to be displayed, end with a semicolon. At this time, the operation is still performed, indicating that it is not displayed.

If only the expression on the right side is left and no variable is assigned, Matlab will automatically give a temporary variable ans and store the operation result in ans.

Basic grammar

Variables or constants in Matlab represent matrices, and scalars should be regarded as matrices of order 1-1.

A = [1 23; 4 56; 7 8 9], elements in the same line are separated by commas or spaces, and different lines are separated by semicolons.

The elements of the variable are indicated by the number (subscript) in parentheses (), and the two-dimensional subscript is separated by a comma.

If the subscript of the assigned element exceeds the size of the original matrix, the rows and rows of the matrix will automatically expand.

Assign values to all rows and use the colon a (5,:) = [5, 4, 3] / / [] to represent the matrix

B = a ([2p4], [1p3]) / / take out the elements at the intersection of rows 2, 4 and column 1, 3 of a to form a new matrix b

A ([2Jing 4jue 5],:) = [] / / extract the 2nd and 4th lines from a, using the concept of empty matrix [], empty matrix is different from zero matrix.

A [2] [4] elements of a (2BI 4) / a matrix

The plural is denoted by I or j.

Z = [1,3] + [2,4 [6,8] * I / /'* 'cannot be omitted

Conjugate transpose

W=conj (z) / / Conjugate

W=conj (z)'/ / transpose

Basic matrix

Zeros (m ~ ~ n), ones (m ~ n), rand (m ~ n), linspace (a, b, n), eye (n) / / linspace (a, b, n) uniformly generate n-point values between an and b, forming 1-dimensional n-dimensional vectors; eye generates unit matrices.

A large matrix can be made up of several small matrices, but it must have the correct number of rows and rows to fill all the elements.

When the display of an element is imprecise, you can use the command to display a single element to get its exact value, or use the long format (format long) to display the entire matrix

[size m] = matrix (z) / / get the order of the matrix. If you want to write the matrix An and B to add and subtract, you must first find nA,mA,nB,mB and check whether it satisfies nA=nB,mA=mB. When multiplying, it's the opposite.

X = [1BI 2,3]; y=x-1

L=length (X) / / one-dimensional array, length has no rows and columns

Inv (z) / / find the inverse matrix, z must be a square matrix of order n

X=inv (D) * bounded D\ B / / multiplies the inverse matrix of D by B, which is denoted as D\, which is called left division.

X=B*inv (D) = Bram D / / multiplies the inverse matrix of D by B, denoted as / D, which is called right division.

*,\, /, ^, expm, logm, sqrtm operate on the matrix as a whole

+, -, sqrt, exp, log are performed on the elements in the matrix respectively (by element group operation)

Extraction and Transformation of Matrix structure

Fliplr matrix flips fliplr (A) left and right, flipud matrix flips flipud (A) up and down, reshape order is reorganized, and the total number of elements remains the same reshape (A, 2,6).

The whole rot90 matrix is rotated 90 degrees counterclockwise rot90 (A), diag extracts or establishes the diagonal matrix diag (A), tril takes the left lower triangular part of the matrix tril (A), triu takes the right upper triangular part of the matrix triu (A),: the elements are taken out in a column to form a column.

Element group operation

Array assignment

1) use two colons to form an equal increment statement in the format: t = [initial value: increment: final value], which can be omitted when the increment is 1.

2) use linspace function, format: linspace (initial value, final value, points)

Element group operation means that all elements in the matrix are operated according to a single element. In order to distinguish it from the operational symbol of the matrix as a whole, add a symbol'. 'before the operators *, /,\, ^ to indicate that the element group operation is being performed. The two matrices involved in the operation must be of the same order. A non-square matrix cannot be exponentiated as a whole.

Eg:

X = [0:0.1:pi/4]'

Disp ('x sin (x) cos (x) tan (x)')

Disp ([cos (x), cos (x), tan (x)])

Logical judgment

1 Mathematical and logical operators (~ = not equal)

2 logical character checking (all, any, find, isnan, isreal, isstr)

3-bit operation

4 set operation

Flow control statement

1 if statement

1) if (expression) section, end

2) if (expression1) section1, elsesection2, end

3) if (expression1) section1, elseifexpression2 section2, else section3, end

2while statement

While (expression) section, end

3 for statement

For k = initial value: increment: final value statement group A, end

4 switch statement

Switch expression (scalar or string)

Case value 1

Statement group A

Case value 2

Statement group B

... ...

Otherwise

Statement group N

End

Basic drawing method

Graph3d,Graph4d,Graphics,specgraph function library

Basic drawing function description:

Plot drawing continuous Waveform stem drawing discrete Waveform subplot drawing multiple graphs in one window

Axis defines the maximum and minimum value of the x _ ray y-axis hold retains the current curve grid draws gridlines xlabel/ylabel as the x _ arrow y-axis plus instructions

Title adds a title to the graph text adds text to the picture gtext uses the mouse to drag to determine the location of the dimension text

1 two-dimensional curve in Cartesian coordinates

1) plot (y) input an array

2) the case of plot (XQuery) entering two arrays

If the array x and y have the same length, the command plot (XMagol y) will draw a curve with the Abscissa of the x element and the ordinate of the y element.

2 linear, dotted, color

Magenta c cyan r red g green b blue w white k black

.: dotted line, -. Dot stroke-- long stroke

Eg: plot (x1, y1,': y'), plot (x2recoery y2recr')

3 the drawing of more than one curve

1) plot (t, [y1, y2, …]) All outputs are required to have the same length and the same independent variable vector, so it is not convenient for users to set their own linetype and color.

2) hold command coordinates should be selected properly, and pay attention to releasing the hold state hold off in a timely manner.

3) use multiple input variables after plot

Plot (x1, y1, etc., x2, y2, etc.)

4) plotyy command

Plotyy (tQuery ypencil t2pr y2)

Screen control

1 control of graphics window

Figure creates a graphics window gcf gets the handle of the current graphics window clf clears the current graphics window shg display graphics

Refresh refresh graphics close close graphics window close all close all graphics windows

2 control of shafting

Axes creates coordinate system anywhere gca gets handle to current coordinate system cla clears current coordinate system ishold keeps current drawing state true

3 drawing object

Line creates lines surface creates surfaces patch creates graphics fillers light creates lighting p_w_picpath creates images

4 graphic handle operation

5 tools

6 others

Subplot: divide the graphics window into nimm subgraphs and draw the graph at the p subgraph.

Other 2D drawing commands

Drawing of imaginary numbers

Setting of coordinate scale and size-axis command

Direct editing in the graphics window

M file and program debugging

There are two types of M files: one is the main program file (script file), which is written by the user to solve a specific problem, and the other is a subroutine, also known as a function file (function file), which must be called by other files

Main program file

1) start with statements such as clear,close all, clear out the original variables and graphics in the workspace, in order to avoid the impact of residual data of other executed programs on this program. The first few lines are usually comments indicating the purpose of this program. Comments must start with%. When you enter help filename in MatLab, the first few lines of the file that start with% are displayed on the screen.

2) the program body. Be careful to avoid Chinese characters, otherwise it will cause a crash

3) do not use Chinese characters for file names. Avoid the occurrence of Chinese character path names.

Human-computer interaction command

Valuation and execution, process control statements, programs, functions and variables, variable management, information display, human-computer interaction commands

The pause (n) program executes here, pauses for n seconds, and then continues. If there is no parenthesis argument, wait for the user to enter any key before continuing.

The execution of the keyboard program is paused here, the character K is displayed on the screen, and the input and execution rights of the program are given to the user. If you need the system to resume running the original program, just enter the string return. Setting this command in the M file is beneficial to program debugging and temporary modification of variable contents.

The command of Ctrl+C to forcibly stop a program from running

Function file

Starting with function, the function name must be the same as the file name

There are input and output variables (variables), which can be passed.

Unless declared with global, variables in the program are local variables and are not saved in the workspace

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report