如何创建一个十字交叉对角矩阵

时间:2016-11-30 19:52:26

标签: matlab matrix

如何创建这样的矩阵

A = [a 0 0 0 0 b;
     0 a 0 0 b 0;
     0 0 a b 0 0;
     0 0 b a 0 0;
     0 b 0 0 a 0;
     b 0 0 0 0 a]  

1 个答案:

答案 0 :(得分:3)

也许是这样的

N = 6; % Size of matrix
a = 1; % Example value
b = 2; % Example value

A = a*eye(N) + b*fliplr(eye(N));

A =
     1     0     0     0     0     2
     0     1     0     0     2     0
     0     0     1     2     0     0
     0     0     2     1     0     0
     0     2     0     0     1     0
     2     0     0     0     0     1