摘要:在MATLAB中,可以直接在命令行或脚本中输入矩阵元素。规则如下:用方括号表示矩阵;同一行元素用逗号“,”或空格分隔;不同行用分号“;”分隔。
分享兴趣,传播快乐,
增长见闻,留下美好!
亲爱的您,这里是LearningYard学苑。
今天小编为大家带来
“颜读(42):MATLAB入门学习之矩阵建立”
欢迎您的访问!
Share interest, spread happiness,
increase knowledge, and leave beautiful.
Dear, this is the LearningYard Academy!
Today, the editor brings the
"Yan Du (42): MATLAB Beginner's Learning: Matrix Creation."
Welcome to visit!
一、思维导图(Mind Mapping)
二、直接输入法(Direct Input Method)
在MATLAB中,可以直接在命令行或脚本中输入矩阵元素。规则如下:用方括号表示矩阵;同一行元素用逗号“,”或空格分隔;不同行用分号“;”分隔。
In MATLAB, you can enter matrix elements directly on the command line or in a script. The rules are as follows: Use square brackets to represent a matrix; separate elements in the same row with commas, "," or spaces; separate elements in different rows with semicolons, ";".
三、使用函数生成特殊矩阵(Generate special matrices using functions)
(1)zeros函数(zeros function)
zero(m,n)表示m行n列的全0矩阵,下图生成3行4列的全0矩阵。
zero(m,n) represents an all-zero matrix with m rows and n columns. The following figure generates an all-zero matrix with 3 rows and 4 columns.
(2)eye函数(eye function)
eye(m,n)表示m行n列的单位矩阵(对角线为1,其余为0),下图生成3行3列的单位矩阵。
eye(m,n) represents the identity matrix with m rows and n columns (the diagonal is 1 and the rest are 0). The following figure generates an identity matrix with 3 rows and 3 columns.
(3)ones函数(ones function)
ones(m,n)表示m行n列的全1矩阵。下图展示了2行3列的全1矩阵。
ones(m,n) represents a matrix with m rows and n columns containing all ones. The following figure shows a matrix with 2 rows and 3 columns containing all ones.
(4)magic函数(magic function)
magic函数用于创建魔方矩阵,即每行、每列和对角线上的元素之和都相等的矩阵。需要注意的是magic(n)中n必须是大等于3的整数。下图为提取魔方矩阵中的元素的具体代码,magic(2,2)表示魔方矩阵中第2行第2列元素,magic(3,:)表示魔方矩阵中第3行所有元素,magic(:,4)表示魔方矩阵中第4列所有元素,magic(1:2,2:4)表示魔方矩阵中第1-2行,第2-4列交汇的元素。
The magic function is used to create a magic cube matrix, that is, a matrix in which the sum of the elements in each row, column, and diagonal is equal. Note that n in magic(n) must be an integer greater than or equal to 3. The following figure shows the code for extracting elements from a magic cube matrix. magic(2,2) represents the element in the second row and second column of the magic cube matrix, magic(3,:) represents all elements in the third row of the magic cube matrix, magic(:,4) represents all elements in the fourth column of the magic cube matrix, and magic(1:2,2:4) represents the elements at the intersection of rows 1-2 and columns 2-4.
(5)diag函数(diag function)
diag函数在MATLAB中用于创建对角矩阵。具体代码如下图所示。
The diag function in MATLAB is used to create a diagonal matrix. The code is shown below.
四、通过向量生成矩阵(Generating matrices from vectors)
使用冒号运算符生成向量,然后组合成矩阵。下图展示了行向量的生成和转置,组合成矩阵的过程。
Use the colon operator to generate vectors and then combine them into matrices. The following figure shows the process of generating and transposing row vectors and combining them into matrices.
学会了的话,不妨就动手试试。
If you've learned it, why not give it a try yourself?
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言,
让我们相约明天。
祝您今天过得开心快乐!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:Google翻译
参考资料:CSDN博客、deepseek、ChatGPT
本文由LearningYard学苑整理发出,如有侵权请在后台留言!
来源:LearningYard学苑