Matlab 对角线元素赋值。

a(1:7:end)=3;%对角线元素赋值为3
自己动手操作了,确实是对角线元素赋值为3,但是不大理解这行代码为什么可以实现对角线元素的赋值,求解

 采纳的回答

如果没猜错的话,你的矩阵a是一个6*6的矩阵,如下
>> a = zeros(6);
>> a(1:7:end)=3
a =
     3     0     0     0     0     0
     0     3     0     0     0     0
     0     0     3     0     0     0
     0     0     0     3     0     0
     0     0     0     0     3     0
     0     0     0     0     0     3
matlab的矩阵有两种索引方式,即单索引和双索引。单索引方式按列排序,a(k)为第k个数,双索引方式按(行、列)索引,如第六行第一列为a(6)或a(6,1)。按照单索引方式, 1:7:end对应的就是对角线的位置。建议认真看一下基础知识关于低维数组的寻址和搜索方面的内容。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!