当前位置:网站首页>[matlab] matrix operation

[matlab] matrix operation

2022-06-12 23:55:00 No card, no change

Matrix operation

Generate matrix

How to generate row matrix :

  1. Colon expressions

    x = 1:1:5 
    % 1 2 3 4 5
    

    In steps 1, from 1 To 5 Generate value , Form row matrix .

    If the step size is not set , The default step size is 1.

  2. linspace(a, b, n)

    linspace(1, 5, 5)
    % 1 2 3 4 5
    

    from 1 To 5 Generate 5 A numerical , Form row matrix .

    from a To b Generate n A numerical , Form row matrix .

    If we do not set up n, The default is 100.

A matrix is generated from a matrix

repmat function

B = repmat(A, m, n): The matrix A As a whole , Horizontal replication m Time , Copy vertically n Time , obtain m × n m×n m×n individual A A matrix formed by a matrix , Return to the new matrix .

B = repmat(A, [m, n]): Same as above , The general usage of this call form is :C = repmat(A, size(B));.

A = randi(10, 2, 3) %  produce [1, 10] Of 2×3 Matrix 
B = repmat(A, 3, 2)

give the result as follows :

A =

     9     4     1
     7    10     5

B =

     9     4     1     9     4     1
     7    10     5     7    10     5
     9     4     1     9     4     1
     7    10     5     7    10     5
     9     4     1     9     4     1
     7    10     5     7    10     5

How matrix elements are referenced

  1. Reference matrix elements by subscript

    A(3, 4) Get matrix A The elements in the third row and the fourth column .

    A(5, 7) = 2 The matrix A The elements in the fifth row and seventh column are set to 2, If the matrix A The previous size was less than five rows and seven columns , Then the matrix is extended , Unassigned elements are set to 0.

  2. Reference matrix elements by ordinal numbers

    Column priority , from 1 Start .

    The sequence number and subscript correspond to each other , With m × n m×n m×n matrix A For example , Matrix elements A(i, j) The serial number of is ( j − 1 ) × m + i (j-1)×m+i (j1)×m+i.

The sequence numbers and subscripts of matrix elements can be used sub2ind and ind2sub Functions convert to each other .

  1. sub2ind function : The row of the specified element in the matrix 、 Convert the column subscript to the stored sequence number . The invocation format is :D = sub2ind(S, I, J)

    among ,S Is a matrix composed of rows and columns ,I A matrix formed for the row coordinates of the matrix elements to be transformed , That is, the coordinates of multiple matrix elements can be converted at one time , alike ,J A matrix consisting of the column coordinates of the matrix elements to be transformed ,D Is the corresponding sequence number matrix . obviously ,I and J Must be a homomorphic matrix .

    A = [1:3;4:6]
    D = sub2ind(size(A), [1 2;2 2], [1 1;3 2])
    

    give the result as follows :

    A =
         1     2     3
         4     5     6
    D =
         1     2
         6     4
    

    size(A) Returns a matrix A The row matrix is composed of the number of rows and the number of columns .

  2. ind2sub function : The sequence number of the matrix element will be converted into the corresponding subscript , Its calling format is :[I, J] = ind2sub(S, D)

    The letters have the same meaning as above .D It can also be a matrix , Got I and J It's also a matrix .

    [I J] = ind2sub([3 3], [1 3 5])
    

    give the result as follows :

    I =
         1     3     2
    J =
         1     1     2
    

Get submatrix

A (i, : ) The first i All elements of the line
A ( : , j) The first j All elements of the column
A( i : i+m , k : k+m ) The first i~i+m Within the line and on page k~k+m All elements in the column
A( i : i+m , : ) The first i~i+m All elements of the line

end Operator : Indicates the subscript of the end element of a dimension .

A = reshape(1:20, [5, 4])'
A1 = A([1 4], 3:end)
A2 = A(end:-1:1, : )

give the result as follows :

A =

     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

A1 =

     3     4     5
    18    19    20

A2 =

    16    17    18    19    20
    11    12    13    14    15
     6     7     8     9    10
     1     2     3     4     5

reshape Function explanation

reshape Function is a Column first function , Whether it's taking numbers from a matrix or adding numbers to a new matrix , All are carried out according to the principle of column priority .

reshape Standard usage of functions :reshape(A, [ROW, COL]) or reshape(A, ROW, COL), On the premise that the total number of elements of the matrix remains unchanged , The matrix A Transform into R O W × C O L ROW×COL ROW×COL Matrix , Return to the new matrix .

How to understand "shape Function is a column first function "?

A = 1:6
A1 = reshape(A, [2, 3])

give the result as follows :

A =

     1     2     3     4     5     6

A1 =

     1     3     5
     2     4     6

take A The number in is 1 The element as A1 The number in is 1 The elements of , take A The number in is 2 The element as A1 The number in is 2 The elements of ,……, take A The number in is 6 The element as A1 The number in is 6 The elements of . This fills the new matrix A1, Because this function assigns values to the new matrix according to the sequence number of the elements , The order of element numbering is column first , So this function is a column first function .

The essence of this function is to change the number of rows and columns of the original matrix , But it does not change the number of elements of the original matrix and its storage order .

another : May refer to


For the above structure 4 × 5 4×5 4×5 A matrix that is incremented by rows , Must first construct 5 × 4 5×4 5×4 Matrix , Then perform the transpose operation . among ,' Is to perform transpose operation .

A1 The matrix is obtained A The first row of the matrix goes from the third column to the fifth column ( That's the last column ) And the fourth row from the third column to the fifth column ( That's the last column ) All the elements of , The corresponding positions of these elements remain unchanged , But the absolute position changes , That is, fill a new matrix with these numbers .

A2 A matrix can be understood as a matrix A Flip the matrix up and down . Take the fourth line first ( The last line ) All the elements of , It is equivalent to putting all the elements of the fourth line in A2 The first line of the matrix , Take all the elements in the third row , It is equivalent to putting all the elements of the third line in A2 The second line of the matrix ,……, Thus, the operation of turning up and down is realized . Note that the step size is set to -1, Otherwise default to 1, The default is 1 It is impossible to realize from end Reduced to 1 Of , So there will be mistakes . This is a special usage or technique .

Delete submatrix

Delete the submatrix by assigning an empty matrix to the submatrix .

A = reshape(1:20, 5, 4)';
A([2 4], :) = []

give the result as follows :

A =

     1     2     3     4     5
    11    12    13    14    15

Our assignment results in A The matrix has lost all the elements of the second and fourth rows , new A A matrix is a 2 × 5 2×5 2×5 Matrix .

If we want to delete the submatrix of A The middle doesn't go with A The first line of 、 The last line 、 Will a submatrix that intersects the first and last columns succeed , If successful, what will the new matrix look like ?

A = reshape(1:20, 5, 4)';
A(2:3, 2:4) = []

give the result as follows :

 An empty assignment can only have one non colon index . % ERROR!

We got a false warning ,“ An empty assignment can only have one non colon index ”, This means that if we want to set the submatrix to an empty matrix , It must be a submatrix composed of several complete columns or rows , That is, at most one non colon index .( You can also understand it as , This is the only requirement , To ensure that the output is a legitimate matrix , If you don't ask , Can you imagine what the output is ?)

Special usage of colon index

B(:) You get the matrix B The column vector formed by the elements of , It is still column priority .

B = [1 2 3; 4 5 6; 7 8 9]
B(:)

give the result as follows :

B =

     1     2     3
     4     5     6
     7     8     9

ans =

     1
     4
     7
     2
     5
     8
     3
     6
     9

It can be seen that B(:) Equivalent to reshape(B, 6, 1) The matrix B Convert to six rows and one column .

therefore , For any matrix A,A(:) Equivalent to reshape(A, ROW×COL, 1).

原网站

版权声明
本文为[No card, no change]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122346054318.html