[MATLAB] basic Tutorial Study Notes (1): basics and settings and matrix tutorial

Source: Internet
Author: User

1. MATLAB setting functions.

Path: displays all content contained in the search path.

CD: current direction view current path

%: Add Comment

Userpath: displays the default path.

Example: userpath ('d: \ Program Files \ MATLAB \ r2008a \ work '); set this directory as the startup directory and the parameter as the path name;

Savepath: Save the modification

Pathtool: interface tool to modify the corresponding search path

CLC: command interface clear screen



Ii. Data Operations

WHO: view the variable name in the Workspace

Whos: displays detailed information about the workspace variable name.

Clear: Clear all variables in the workspace, or clear all variables without Parameters

Save: Save all variables in the workspace to the local mat file.

Usage: save [file name] [variable name] [-append] [-ASCII]

Use instance (store data d): Save mymatlab D-append

Load: load the mat local file name without a suffix

Format: Format + format, which only affects storage. The default value is short.



Iii. Data Types

Numeric data

A = 1; create data of the double type.

B = uint8 (a); convert a to an unsigned 8-bit integer

Use class to view the current data type

ASCII code of character a output by the double or ABS Function

The Char function converts an ascii code to a character output.

Str2num and num2str enable interchange between strings and values

Eval treats the string as a MATLAB statement for execution


Struct

Define a struct:

A. x = 1

A. Y = 2

A. z = 3

Isstruct checks whether it is a struct. If it is a struct, 1 is returned.

Fieldnames displays all the members of the struct.

Isfiled (A, 'x') checks whether it is a struct Member

Rmfield deletes struct members.

Getfield obtains the struct member.


Unit

Use {} to create

A = {1, 'string', [1, 2, 4]}


III. Basic operations on Matrices

Matrix Creation

1) directly create:

Spaces are separated by commas,

A={1 2 3;4 5 6;7 8 9};B={1 2 3;4 5 6;7 8 9};C=A';

A semicolon indicates that it is not a vector of the same row.


2) file creation:

Run the edit command to create a M file. If you run the command, enter the file name.


3) special matrix:

Zero matrix: zero matrix of three rows and four columns of zeros.

Matrix: The matrix of the ones (3, 4) three rows and four columns.

>> ones(3,4)ans =     1     1     1     1     1     1     1     1     1     1     1     1


4) create a vector using a colon expression

E1: E2: E3 Initial Value: Step Size: end value

>> 1:2:7ans =     1     3     5     7




5) linspace (A, B, n)

A: The first element. B: The last element. N: Total Element

It can be exchanged with a colon expression.

>> linspace(1, 5, 3)ans =     1     3     5



Simple operations on Matrices

1) index matrix: Index Using subscript.

A (2, 3): First mark, followed by column mark.

A (5): return sequence. MATLAB is used for column storage, so vertical counting.

[M, N] = find (A = 8): Two Parameters return the row number and column number.

IND = find (A = 8): returns the serial number of a parameter.

Size (a): returns the size of the matrix.

>> A=[1 2 3;4 5 6;7 8 9]A =     1     2     3     4     5     6     7     8     9>>  size(A)ans =     3     3

Sub2ind (matrix size, row mark, list): Convert tables M and N into sequence numbers.

[M, N] = ind2sub (matrix size, sequence number): Convert the sequence number to the following table


2) rearrange the Matrix

H = reshape (matrix A, number of rows 9, number of Columns 1) rearranged

>> H=reshape(A,1,9)H =     1     4     7     2     5     8     3     6     9

H = a (:); convert a matrix into a column vector and return


3) transpose the Matrix

Add a single quotation mark to convert rows into columns and columns into rows.


4) matrix splitting

M = A (1, :) all elements in the first row. The colon represents all columns.

M = a (: End) first column of the First row to the last column

M = A (1, [1 2 3]) the first and third columns of the first row.

M = a (: 3) the first column of the first row, the second column and the third column.

M = a ([1, 2], [1 2]) first and second columns in the second row of the first row.


5) delete a Matrix

Assign a null value.

A (2) = []: Delete the second element in matrix.

A (1, :) = []: deletes the first row element in matrix.




6) extended Matrix

Repmat (A, 2, 1) Copies two rows and one column

>> AA =     1     2     3     4     5     6     7     8     9>> repmat(A,2,3)ans =     1     2     3     1     2     3     1     2     3     4     5     6     4     5     6     4     5     6     7     8     9     7     8     9     7     8     9     1     2     3     1     2     3     1     2     3     4     5     6     4     5     6     4     5     6     7     8     9     7     8     9     7     8     9

7) compression Matrix

Delete the same element to compress the matrix: Unique ()




IV. Creation of special matrices

1) Unit Matrix

Eye (m, n): Creates a matrix of units. Generally, the number of rows equals the number of columns, that is, M = n.

>> eye(5,5)ans =     1     0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1     0     0     0     0     0     1


2) random matrix

Rand generates 0 ~ Random matrix of 1, random matrix: S = rand ):

>> S=rand(10,1)S =    0.8147    0.9058    0.1270    0.9134    0.6324    0.0975    0.2785    0.5469    0.9575    0.9649


Randn generates a random matrix with a mean of 0 and a variance of 1. Random matrix: S = randn ):

>> S=randn(1,10)S =    0.6715   -1.2075    0.7172    1.6302    0.4889    1.0347    0.7269   -0.3034    0.2939   -0.7873

To generate a random vector with an even distribution from A to B, use: S = a + (B-a) * rand (m, n ):

>> S = 5 + (7 - 4)*rand(5,1)S =    5.9513    7.8507    5.1033    6.3162    6.1447

A random matrix that generates a normal distribution with the mean U variance of S: S = u + SQRT (s) * randn (m, n ):

>> S = 7 + sqrt(3)*randn(5,1)S =    5.0132    5.1487    5.5979    1.9004    9.4913

Mean and STD can be used to calculate the mean and variance.


3) Cube Matrix

Use magic (n) to generate a cube matrix:

>> magic(5)ans =    17    24     1     8    15    23     5     7    14    16     4     6    13    20    22    10    12    19    21     3    11    18    25     2     9


4) Hilbert Matrix

Hilb (n) generates the Hilbert matrix:

>> hilb(5)ans =    1.0000    0.5000    0.3333    0.2500    0.2000    0.5000    0.3333    0.2500    0.2000    0.1667    0.3333    0.2500    0.2000    0.1667    0.1429    0.2500    0.2000    0.1667    0.1429    0.1250    0.2000    0.1667    0.1429    0.1250    0.1111>> format rat>> ansans =       1              1/2            1/3            1/4            1/5            1/2            1/3            1/4            1/5            1/6            1/3            1/4            1/5            1/6            1/7            1/4            1/5            1/6            1/7            1/8            1/5            1/6            1/7            1/8            1/9     


5) Toeplitz matrix

>> toeplitz(1:6)ans =     1     2     3     4     5     6     2     1     2     3     4     5     3     2     1     2     3     4     4     3     2     1     2     3     5     4     3     2     1     2     6     5     4     3     2     1


V. Matrix and vector operations

% Calculation of matrix vectors A = [1 0 0 0; 3 1 0 0;-5 2 1 0; 7 3 2 1]; B = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7]; k = 3; C = a + B; D = K * A; E = '; det (E); % calculate the determinant inv (E); % calculate the inverse matrix


Use det to determine the deciding factor first. If the determining factor is not 0, use INV to obtain its inverse matrix.

Vectors can be regarded as 1 * n matrices or N * 1 matrices. Therefore, addition and multiplication operations of vectors are the same as those of matrices.

Inner Product Operation:

A = [1 + 5I, 2, 3 + 6i, 7-2I]; B = [2-I, 4 + 3I, 3-I, 6]; S = sum (conj (B ). * A) S = A * B '% solve AB Inner Product S = dot (B, A) % solve AB Inner Product


6. Linear Equations

% Solution of Linear Equations A = [1, 2, 3; 1, 4, 9; 1 8 27]; B = [5,-2, 6] '; X = inv (a) * B % efficiency not high X = A \ B


7. Similarity simplification and decomposition of Matrices

% Similarity simplification and decomposition of a matrix A = [0 3 3;-1 8 6; 2-14-10]; Jordan (a) [v j] = Jordan () A = [1 0 I; 0 2 0;-I 0 1]; EIG (a) [e d] = EIG ()

8: norm

% Norm A = [0 3 3;-1 8 6; 2-14-10]; norm (A, 1) norm (A, 2) norm (A, INF) norm (A, 'fro ')

9. Matrix Analysis

% Matrix analysis Syms XA = [sin (x) exp (x) 1; cos (x) x ^ 2 + 1 log (x)]; diff () diff (A, 2) a = [0 1; 0-2]; expa = funm (A, @ exp) expa = expm (a) Sina = funm (, @ sin) cosa = funm (A, @ cos)

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.