矩阵是元素的二维数组。在MATLAB中,通过分配由空格或逗号分隔的数组元素并使用分号标记每行的结尾来创建矩阵。现在,让我们看一些示例以更好地理解它。
句法:
a = [elements; elements]
示例:创建矩阵
MATLAB
% MATLAB program to create
% a matrix
%Number Matrix
x = [1 2 3;4 5 6;7 8 9]
%String Matrix
y = ['Geeks';'Geeks']
MATLAB
% MATLAB program to know
% the size of a matrix
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
xSize = size(x)
y=['Geeks';'Geeks'];
ySize = size(y)
MATLAB
% MATLAB program to access
% a particular element
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
x(3,2)
y=['Geeks';'Geeks'];
y(1,2)
MATLAB
% MATLAB program to access
% a multiple elements
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
% Accessing all rows and columns
x(:,:)
% Accessing all the elements
% in the first two rows
x(1:2,:)
% Accessing all the elements
% from 2nd to last element
% in every row.
x(:,2:end)
% Accessing elements from 2nd index
% to 3rd index in the first two columns
x(1:2,2:3)
MATLAB
% MATLAB program to add
% dimensions to a matrix
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [7 8 9 10;11 12 13 14];
% Concatinating two matrices
% or adding a row
a = [x;y]
b = [x;11 12 13 14]
%Adding a column to a matrix
x(:,5)=[15 16]
MATLAB
% Matlab Program to
% explain concatenation
x = [1 2 3 4;4 5 6 7];
% Creating a new matrix by
% concatinating multiple rows
% of a matrix
a = x([1,2,2,1],:)
% Creating a new matrix by
% concatenating multiple columns
% of a matrix
b = x(:,[1,3,2])
MATLAB
% MATLAB program to concatenate
% two matrices using cat() funtion
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [1 2 3 4;4 5 6 7];
% Concatenating two matrices
% Using cat() function
a = cat(1,x,y)
b = cat(2,x,y)
MATLAB
% MATLAB program to delete
% a row or a column
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
% Deleting 3rd row of
% a matrix
x(3,:) = []
% Deleting 3rd column in
% every row of a matrix
x(:,3) = []
输出:
示例:知道矩阵的大小
的MATLAB
% MATLAB program to know
% the size of a matrix
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
xSize = size(x)
y=['Geeks';'Geeks'];
ySize = size(y)
输出:
访问矩阵的元素
为了引用矩阵中的元素,我们编写了matrix(m,n)。这里,m和e是行和列的索引。
范例1:
的MATLAB
% MATLAB program to access
% a particular element
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
x(3,2)
y=['Geeks';'Geeks'];
y(1,2)
输出:
为了访问矩阵中的多个元素,我们编写
matrix(x:y;,xy)
范例2:
的MATLAB
% MATLAB program to access
% a multiple elements
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
% Accessing all rows and columns
x(:,:)
% Accessing all the elements
% in the first two rows
x(1:2,:)
% Accessing all the elements
% from 2nd to last element
% in every row.
x(:,2:end)
% Accessing elements from 2nd index
% to 3rd index in the first two columns
x(1:2,2:3)
输出:
向矩阵添加元素/尺寸
要将元素/维度添加到矩阵,我们可以使用以下方法之一:
cat(dimension,A,B,….)
or
x = [A;B]
在上面的语法中,A和B是我们需要传递到函数cat()或通过使用方括号进行连接的矩阵。对于表或时间表输入,此处的维参数必须为1或2。
范例1:
的MATLAB
% MATLAB program to add
% dimensions to a matrix
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [7 8 9 10;11 12 13 14];
% Concatinating two matrices
% or adding a row
a = [x;y]
b = [x;11 12 13 14]
%Adding a column to a matrix
x(:,5)=[15 16]
输出:
范例2:
的MATLAB
% Matlab Program to
% explain concatenation
x = [1 2 3 4;4 5 6 7];
% Creating a new matrix by
% concatinating multiple rows
% of a matrix
a = x([1,2,2,1],:)
% Creating a new matrix by
% concatenating multiple columns
% of a matrix
b = x(:,[1,3,2])
输出:
范例3:
的MATLAB
% MATLAB program to concatenate
% two matrices using cat() funtion
% Creating matrices
x = [1 2 3 4;4 5 6 7];
y = [1 2 3 4;4 5 6 7];
% Concatenating two matrices
% Using cat() function
a = cat(1,x,y)
b = cat(2,x,y)
输出:
示例4:删除矩阵的行/列
的MATLAB
% MATLAB program to delete
% a row or a column
% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
% Deleting 3rd row of
% a matrix
x(3,:) = []
% Deleting 3rd column in
% every row of a matrix
x(:,3) = []
输出:
笔记:
- 与大多数编程语言不同,MATLAB数组索引从1开始。
- 要对矩阵执行(几乎所有)运算,尺寸必须相同。
- 为了避免出现多个输出,必须在语句的每一行末添加分号。
- 在MATLAB中,字符串是一个字符数组。
- 在MATLAB中, length()给出数组的长度, size()给出矩阵的大小。