Matlab matrix/cell array generation, transformation and operation commands

Foreword:  

    Matlab has been used for many years, and there are a lot of tips for using matrices that are often forgotten, and it is troublesome to find a new one. So to sum up, the explanation of some professional functions in the article is written with reference to the explanation of others, and there may be repetitions. Wang Haihan . If you are helpful to everyone, you can give a thumbs up, which is considered to be an endorsement of my code word. Thank you everyone. I hope you can correct me if there is a mistake!

1. The use of matrices and cell arrays

    There are two main array storage methods commonly used in Matlab, matrix and cell

    1. When the data is a single (multiple) group of one-dimensional data and a single group of two-dimensional data , use a matrix/one-dimensional array for storage

     E.g:

                         

    2. When the data is multiple sets of two-dimensional data , use a cell array to store multiple matrices at the same time

    E.g:

               

Second, the generation of matrices and cell arrays

    The matrix generation methods mainly include data reading, random generation, 0/1 generation, and the generation of special matrices (unit matrix, etc.). I believe everyone is familiar with data reading. You can use functions such as xlsread. Kind of matrix generation method. This includes the generation of one-dimensional arrays, which are generally not distinguished when I use them, so I did not say separately.

    1. Random Matrix

        rand: Generate a pseudo-random number with a mean value of 0.5 and an amplitude between 0 and 1

                  A=rand(n): Generate an n-order random number square matrix A between 0 and 1

                  A=rand(m,n): Generate an m×n random number matrix A between 0 and 1

        randn: Generate a normally distributed random number with a mean value of 0 and a variance of 1.

                  Similarly, A=randn(n) is to generate square matrix, A=randn(m,n) is to generate m×n matrix

    2. 0/1 matrix

        zeros: Generate a matrix of all 0s

                  A=zeros(n): Generate a square matrix with all zeros

                  A=zeros(m,n): Generate an m×n matrix of all 0s

        ones: Generate a matrix of all ones

                  Similarly, A=ones(n) generates a square matrix of all 1, and A=ones(m, n) generates an m×n matrix of all 1.

    3. Identity matrix

         A=eye(m,n): generate m×n identity matrix

         A=diag(a,k): Generate a matrix with the diagonal element of the k-th row being a and other elements being 0. When k=0, the main diagonal is the main diagonal, and when k=1, the main diagonal is a diagonal Line, -1 is the diagonal of the next layer.

         For example: A=diag([1,1,1],0) A=diag([1,1,1],1) A=diag([1,1,1],-1)

                                        

        The method of replacing the k-layer diagonal elements in the A matrix with a specific matrix :

        Replace the diagonal value of the next layer (k=-1) of the main diagonal in the A matrix with [5,6].

A=ones(3);
A=A-diag(diag(A,-1),-1)+diag([5,6],-1)

    4. Array in order/reverse order

         The generation method is A=(start value; growth length (can be negative growth) ; end value)

         A=(1:1:10): A=[1,2,3,4,,6,7,8,9,10], can be abbreviated as A=(1:10) The default increase unit is 1
         A= (10:-1:1): A=[10,9,8,7,6,5,4,3,2,1]

    5. Cell array

         A=cell(n): Generate an n×n cell array of null values

         A=cell(m,n): Generate an m×n cell array of null values, similar to a matrix

         A={X,Y,Z} : Generate cell array {[X],[Y],[Z]}, where X, Y, Z can be specific matrices and matrix symbols

         For example: A={[1,2],[3,4],[5,6]}

         

3. Operations on matrices and cell arrays

    1. The splicing of matrix

          A=[B,C]: Put the C matrix behind the B matrix, A=[[1,2],[3,4]] is [1,2,3,4]

          A=[B;C]: Place C matrix on the next row of B matrix, A=[[1,2];[3,4]] is [1,2;3,4]. Note: The splicing row matrix needs to have the same dimensions, otherwise it cannot be spliced! Can make 0

          A=reshape(A,n,m): (m×n elements must be satisfied in A) Read the elements in A according to columns, and then splice them according to rows

          For example, change A from 3 rows and 4 columns to 4 rows and 3 columns, read and store in column order

A=[[1,2,3,4];[5,6,7,8];[9,10,11,12]];
A=reshape(A,4,3)

          

         Change it to read in row order and store in column order

A=[[1,2,3,4];[5,6,7,8];[9,10,11,12]];
A=reshape(A',4,3)

    2. Ordinary operations of matrices

        In the symbolic operation of the matrix, * should be replaced by .*, ^ should be replaced by .^, and +,-and / are not required.

        E.g:

A=[1,2,3,4];
B=[5,6,7,8];
C=A.*B

         

    3. Operations on cell arrays

        The operation of the cell array can be regarded as the matrix operation after the matrix is ​​extracted from the cell, so only the extraction of the matrix is ​​introduced.

        For example: extract the m-th row and n-th column of the matrix in the cell: a={m,n}. Braces are required , otherwise the extracted matrix is ​​not a matrix.

A={[1,2,3],[4,5,6],[7,8,9]};
a=A{1,1}

            

    Mr. Zero Zero has written so much, and it took him a whole morning. I hope everyone can like it after reading it. Please trouble everyone~ I will update the code of various algorithms in the future, and you can chat with me if you need it. ~

Guess you like

Origin blog.csdn.net/weixin_41971010/article/details/107149457