4. Introduction to MATLAB—vectors and matrices

Article directory

Preface

1. Array

2. Vector

3. Matrix

3.1. How to create a matrix

 3.2. Matrix transformation

 3.3. Matrix operations

Summarize


Preface

        We know that the full name of MATLAB is MATrix LABoratory. It can be seen that the original intention of AMTLAB is for matrix operations; even if it is a constant, it can be treated as a 1*1 matrix. MATLAB is also good at handling constant operations. Arrays and matrices are very similar. Vectors are one-dimensional arrays, and matrices are two-dimensional arrays. However, in MATLAB, the operations of arrays and matrices are completely different. This must be clarified!


1. Array

        An array is a combination of a series of elements. In MATLAB, its appearance is no different from a matrix, but they represent two completely different variables. Arrays also have the concept of rows and columns, but operations between arrays are usually operations between elements, unlike matrices. The matrix is ​​a whole, and the operation rules must be carried out according to the rules of linear algebra. The difference in operations between arrays and matrices is mainly reflected in the operators. Pay attention to whether the operations are between elements or between wholes.


2. Vector

Methods of constructing vectors: ① direct construction method; ② incremental method; ③ linspace/logspace function method;

① Direct construction method: As shown below, commas or spaces in the middle are the same;

② Incremental method: As shown in the figure below, vector A is a row vector from 0 to 5, with an increment of 1; vector B is a row vector from 0 to 6, with an increment of 2;

  ③ Linspace/logspace function method: Use the linspace function to construct a vector, and you need to specify the first and last values ​​​​and the number of elements; the basic form is x = linspace (first, last, num), where first, last and num represent the first and last elements and the number of elements respectively. .

        To use the logspace function to construct a vector, you need to specify the first and last values ​​​​and the number of elements; x = logspace(first,last,num), where the first and last value sizes are 10^first and 10^last, and the default is num = 50;


3. Matrix

3.1. How to create a matrix

① Simple creation method: write the matrix directly, separate different columns with spaces or commas, and separate different rows with semicolons;

 ② The meshgrid function creates a matrix:

The syntax format is: [X,Y] = meshgrid(x,y) Note: x and y represent row vectors and column vectors

                      [X,Y] = meshgrid(x)

                      [X,Y,Z] = meshgrid(x,y,z)

                      [X,Y,Z] = meshgrid(x)

 ③ Construct special matrices: MATLAB provides multiple functions for constructing special matrices, including:

        ① ones: Create a matrix with all elements equal to 1;

        ② zeros: Create a matrix in which all elements are 0;

        ③ eye: Create a matrix with a diagonal of 1 and other elements of 0;

        ④ accumarray: Assign elements of the input matrix to specified positions in the output matrix;

        ⑤ diag: Create a diagonal matrix based on vectors;

        ⑥ magic: Create a magic cube matrix in which the sum of the elements in the rows, columns and diagonals are equal;

        ⑦ rand: Create a matrix or array whose elements are random numbers that obey a uniform distribution;

        ⑧ randn: Create a matrix or array whose elements are random numbers that obey normal distribution;

        ⑨ randperm: Create a vector (1*n matrix);

 ④ Aggregation matrix: An aggregation matrix is ​​a new matrix formed by connecting one or more matrices. The symbol [] is not only a matrix construction symbol but also a MATLAB platform aggregation operator. For example, C = [AB] means aggregating matrices A and B in the horizontal direction, and C = [A;B] means aggregating matrices A and B in the vertical direction;

⑤ Construct a three-dimensional matrix:

 3.2. Matrix transformation

① reshape function: such as reshape(M,2,6), based on the M matrix, arranged by columns, to create a matrix with 2 rows and 6 columns; note that when using the reshape function to generate a matrix, the number of elements of the two matrices must be the same , here M is a matrix with 3 rows and 4 columns, with a total of 12 elements. The matrix of 2 rows and 6 columns generated by reshape also has 12 elements.

 ② flip function: matrix flip, which can be flipped up and down or left and right; filpud is flipped up and down, and filplr is flipped left and right.

 ③ rot function: such as rot90(M), which means rotating the matrix M 90 degrees counterclockwise;

 ④ repmat function: For example, repmat(A,2,3) means copying matrix A, 2 times in the row vector and 3 times in the column vector;

 ⑤ diff function: find the difference, the difference between the next element and the previous element;

 3.3. Matrix operations

        Matrix and array operations are different. Arrays emphasize operations between elements, while matrices are performed using the operation rules of linear algebra. The matrix is ​​a whole, and operations must be performed as a whole. In order to better understand the differences in operations between matrices and arrays, you can distinguish them based on the different operators.

array

+

-

.*

.\

./

.^

.’

matrix

+

-

*

\

/

^

significance

Add

Subtract

Multiply

divide right

Divide left

power

Transpose

       According to this table, we can see that except for the addition and subtraction symbols, the operations on arrays and matrices are different. We can identify whether the variable is an array or a matrix based on the operation symbols. At the same time, we also need to pay attention when programming. Don’t Mistaken.

        In addition to matrix operations, there are also operations within an array, including:

For example v=[-5 1 5 -3], then

        ① min(v): Find the smallest element in this array, return -5;

        ② max(v): Find the maximum element in this array and return 5;

        ③ sign(v): Take the sign of the element in this array and return -1 1 1 -1;

        ④ sum(v): Find the sum of the elements in the array, return -5+1+5-3 = -2;

        ⑤ abs(v): Find the absolute value of the elements in the array, return 5 1 5 3;

        ⑥ prod(v): Find the product of the elements in the array, return -5*1*5*(-3) = 75;


Summarize

        The above is what I have learned today, mainly about vectors and matrices, including the creation methods of vectors and matrices in MATALB, matrix transformations and operations, etc. There are many function commands used and need to be slow. Slow digestion, let’s work together! ! !

Guess you like

Origin blog.csdn.net/weixin_62912626/article/details/132211561