当前位置:网站首页>[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 :
Colon expressions
x = 1:1:5 % 1 2 3 4 5In steps 1, from 1 To 5 Generate value , Form row matrix .
If the step size is not set , The default step size is 1.
linspace(a, b, n)
linspace(1, 5, 5) % 1 2 3 4 5from 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
Reference matrix elements by subscript
A(3, 4)Get matrix A The elements in the third row and the fourth column .A(5, 7) = 2The 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.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 (j−1)×m+i.
The sequence numbers and subscripts of matrix elements can be used sub2ind and ind2sub Functions convert to each other .
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 ,
SIs a matrix composed of rows and columns ,IA 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 ,JA matrix consisting of the column coordinates of the matrix elements to be transformed ,DIs the corresponding sequence number matrix . obviously ,IandJMust 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 4size(A)Returns a matrix A The row matrix is composed of the number of rows and the number of columns .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 .
DIt can also be a matrix , GotIandJIt'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).
边栏推荐
- 【Matlab】矩阵操作
- Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
- 【Matlab】矩阵变换与矩阵求值
- NCF 的Dapr应用实例的运行
- SAP 业务技术平台(BTP) Workflow(工作流)功能介绍
- PLC也能制作小游戏----Codesys编写猜数字小游戏
- [hcie discussion] STP-A
- Running of NCF dapr application instance
- 2022 R2 mobile pressure vessel filling test questions and online simulation test
- The PMP examination time in March 2022 is set -- "March 27"
猜你喜欢

MySql索引

Common message oriented middleware selection

36 krypton's debut | "osogena" won nearly ten million angel rounds of financing. The original DLR scientists of German Aerospace Research and development system modeling and simulation CAE software PA

Tableau

基于Three.js海上风电数字孪生三维效果

你真的会用PostGIS中的buffer缓冲吗?

【Matlab】二维曲线

PLC也能制作小游戏----Codesys编写猜数字小游戏

How to load 100000 pieces of data in leaflet

【Matlab】矩阵变换与矩阵求值
随机推荐
2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne
Start of u-boot S analysis (IV)
Is divicon still used in leaflet in H5 era?
实战 | UI 自动化测试框架设计与 PageObject 改造
【HCIE论述】组播IGMP-A
Test platform series (97) perfect the case part
leaflet中如何优雅的解决百度、高德地图的偏移问题
最全预告!华为云精彩议程火速收藏
Start blogging
Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍
Will the salary increase after obtaining PMP certification?
【Matlab】矩阵操作
在 Golang 中构建 CRUD 应用程序
How leaflet gracefully displays the bubble window of overlapping points
同花顺开证券账户怎么样?到底安不安全呢
2022 operation of simulated examination platform for hoisting machinery command certificate
H5時代leaflet中還在用DivIcon?
Online examination questions for September examination of financial management
Kaust:deyao Zhu | value memory map: a graph structured world model based on off-line reinforcement learning