Matlab matrix common operation method

The basic format of data in matlab is a matrix, and row vectors, column vectors, and scalars are all special cases of matrices. A matrix can be two-dimensional or multidimensional.

(1) Find the elements in the matrix

① find function

In matlab, you can call the find function to find elements that meet certain conditions in the matrix. The common calling format is as follows:

  • ind=find(X)
  • [m n]=find(X)

Where X is the matrix to be searched; ind is the linear index value in matrix X that satisfies the search condition. Because in matlab, the matrix is ​​stored by column, the value of ind indicates the position of the element when it is stored by column in the matrix. m and n are column vectors, which store the row subscript and column subscript of the position of the element in the matrix, respectively.

For example

A=[1 2 3 4;5 6 7 8];
%查找A中大于3的元素,返回元素的索引
ind=find(A>3);

The results obtained are as follows: 

 

How to understand it?

You can also write like this 

A=[1 2 3 4;5 6 7 8];
[m n]=find(A>3);

How to understand this? 

 

Well, this is the understanding and use of the find function

②ind2sub and sub2ind functions

These two functions convert between linear index values ​​and row and column subscripts. The format of the call is as follows

  • [I,J]=ind2sub(size,IND)
  • IND=sub2ind(size,I,J)
A=[1 2 3 4;5 6 7 8];
ind=find(A>3);
[m n]=find(A>3);
[I J]=ind2sub(size(A),ind);
IND=sub2ind(size(A),I,J);

 

 (2) Delete the specified element in the matrix

If you want to delete the specified elements in the matrix, you only need to assign these elements to be empty ("[ ]"). For example, assuming that A is an m*n-dimensional matrix, you can use the following command to delete the specified elements in the matrix:

A(sub2ind(size(A),i,j))=[] %删除A的第i行,第j列的元素
A(i,:)=[] %删除A的第i行的数据
A(i:j,:)=[] %删除A的第i行到第j行的数据
A(:,j)=[] %删除A的第j列的所有元素
A(:,i:j) %删除A的第i列到第j列的数据

For example

A=[1,2,3,4;5,6,7,8];
A(1,:)=[] %删除第1行的所有元素

 

 

A=[1,2,3,4;5,6,7,8];
A(:,1)=[] %删除A的第一列元素

 

For the deletion of a single element in a matrix, MATLAB only allows the use of a linear index value to specify that element. 

(3) Get the specified element in the matrix

Users can use the following methods to get the elements of a certain (some) row or column in the matrix:

X=A(i,:) %取得A的第i行的数据,并赋值给变量X
X=A(i:j,:) %取得A的第i行到第j行数据,并赋值给变量X
Y=A(:,j) %取得A的第j列的数据
Y=A(:,i:j) %取得A的第i列到第j列的数据
Z=A(i:j,n:m) %取得矩阵第i行到第j行以及第n列到第m列之间的数据

(4) Query the size of the matrix

num=size(A) %返回矩阵的行数和列数,num是一个1*2的数组,第一个数值是矩阵的行数,第二个数值是矩阵的列数
num=length(A) %返回A的行数和列数的最大值,相当于max(size(A))
num=size(A,1) %返回矩阵A的行数
num=size(A,2) %返回矩阵A的列数

(5) Obtain the maximum and minimum values ​​of the elements in the matrix: max and min functions

C=max(A) %取得矩阵A中每一列的最大值,组成行向量返回给C
C=max(A,B) %取得矩阵A和B对应元素的最大值
C=max(A,[],dim) %取得矩阵每行或每列的最大值,dim=1表示每列的最大值组成的行向量,dim=2表示每行的最大值组成的列向量
%同理,min也有上面的三种用法

 For example

a=[2 3;3 6;4 9]
b=[1 4;4 5;5 8]
max(a)
min(a)
max(a,b)
max(a,[],2)
max(a,[],1)

 The execution results are as follows

 

Guess you like

Origin blog.csdn.net/yangSHU21/article/details/131143906