当前位置:网站首页>[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 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.
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
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.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 ,
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
andJ
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 .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 , GotI
andJ
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)
.
边栏推荐
- Actual combat | UI automation test framework design and pageobject transformation
- 机加工行业MES系统模具行业MES系统CNCl中工行业MES系统MES扫码报工MES数据采集
- How does idea switch the interface to Chinese
- Is the PMP training organization an actual training?
- 利率降低导致债券价格上涨
- Why study PMP?
- How to load 100000 pieces of data in leaflet
- PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
- KConfig
- 实战 | UI 自动化测试框架设计与 PageObject 改造
猜你喜欢
數組
Matlab [path planning] - UAV drug distribution route optimization
一篇文章学会子网划分
2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試
What can PMP bring to you
线上真实排队系统重构案例分享——实战篇
如何实现OSM地图本地发布并自定义配图
[hcie discussion] multicast igmp-a
2022 R2 mobile pressure vessel filling test questions and online simulation test
Operation of simulation test platform for G3 boiler water treatment test questions in 2022
随机推荐
PLC peut également faire des jeux - - codesys écrit des jeux de devinettes numériques
【Matlab】多项式计算
21 Chundong University blasting safety online peacetime operation 123 [standard answer]
How to use Huawei cloud disaster tolerance solution to replace disaster recovery all-in-one machine
Explanation and practice of implicit transformation and implicit parameters in Scala
2022年3月11日记:王老师的春天,奇异的模板模式
Tsinghua University image source will cause tensorflow GPU installation failure
Start of u-boot S analysis (III)
Common message oriented middleware selection
2022 R2 mobile pressure vessel filling test questions and online simulation test
2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試
QT actual combat case (38) -- using qpprocess class to realize the function of starting process
Lower interest rates lead to higher bond prices
【Matlab】矩阵变换与矩阵求值
How to pass the PMP review?
[hcie discussion] STP-A
SAP Business Technology Platform (BTP) workflow function introduction
Will the salary increase after obtaining PMP certification?
机加工行业MES系统模具行业MES系统CNCl中工行业MES系统MES扫码报工MES数据采集
【Matlab】矩阵操作