如何创建对角矩阵

时间:2014-05-29 11:23:55

标签: matlab matrix

我有2个8x8矩阵,即P0& P1,

P0 = [a a a a a a a a            P1 = [b b b b b b b b
      a a a a a a a a                  b b b b b b b b
      a a a a a a a a                  b b b b b b b b   
      a a a a a a a a                  b b b b b b b b    
      a a a a a a a a                  b b b b b b b b
      a a a a a a a a                  b b b b b b b b
      a a a a a a a a                  b b b b b b b b
      a a a a a a a a]                 b b b b b b b b]

如何在matlab中创建这样的矩阵H?

H = [P0 P1 0   0   0   0
     0  P0 P1  0   0   0
     0  0  P0  P1  0   0
     0  0  0   P0  P1  0
     0  0  0   0   P0  P1]

3 个答案:

答案 0 :(得分:1)

由于P0P1是矩阵,您可以将它们存储在单元格中,H5x6单元格数组/矩阵。

以下是一个例子。

>> P0 = ones(8)
P0 =
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
>> P1 = ones(8) + 1
P1 =
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
>> H = cell(5,6)
H = 
    []    []    []    []    []    []
    []    []    []    []    []    []
    []    []    []    []    []    []
    []    []    []    []    []    []
    []    []    []    []    []    []
>> [H{:}] = deal(0)
H = 
    [0]    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]    [0]
    [0]    [0]    [0]    [0]    [0]    [0]

>> H(sub2ind(size(H), 1:size(H,1), 1:size(H,1))) = {P0}
H = 
    [8x8 double]              [0]              [0]              [0]              [0]    [0]
              [0]    [8x8 double]              [0]              [0]              [0]    [0]
              [0]              [0]    [8x8 double]              [0]              [0]    [0]
              [0]              [0]              [0]    [8x8 double]              [0]    [0]
              [0]              [0]              [0]              [0]    [8x8 double]    [0]
>> H(sub2ind(size(H), 1:size(H,1), 2:size(H,2))) = {P1}
H = 
    [8x8 double]              [8x8 double]              [0]              [0]              [0]    [0]
              [0]    [8x8 double]              [8x8 double]              [0]              [0]    [0]
              [0]              [0]    [8x8 double]              [8x8 double]              [0]    [0]
              [0]              [0]              [0]    [8x8 double]              [8x8 double]    [0]
              [0]              [0]              [0]              [0]    [8x8 double]    [8x8 double]

>> H{1,1}
ans =
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
>> H{1,2}
ans =
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2

sub2ind函数的用法,如果经常与线性索引的概念紧密相关。这里,sub2ind用于查找将值赋给H的对角线字段所需的线性索引。

您可以在此处找到相关文档:

cell arrays

linear indexing

the sub2ind function

答案 1 :(得分:0)

我愿意:

a = 1;
b = 2;

N = 8;
H_width = 6;
H_height = 5;

P0 = ones(N)*a;
P1 = ones(N)*b;

P = [P0,P1];

H = zeros(N*H_height, N*H_width);

for hh = 1:min(H_height,H_width-1)
    H((hh-1)*N+1:hh*N,(hh-1)*N+1:(hh+1)*N) = P;
end

if H_width < H_height
    H((H_width-1)*N+1:H_width*N,end-N+1:end) = P0;
end

N = 2

的示例
 1     1     2     2     0     0     0     0     0     0     0     0
 1     1     2     2     0     0     0     0     0     0     0     0
 0     0     1     1     2     2     0     0     0     0     0     0
 0     0     1     1     2     2     0     0     0     0     0     0
 0     0     0     0     1     1     2     2     0     0     0     0
 0     0     0     0     1     1     2     2     0     0     0     0
 0     0     0     0     0     0     1     1     2     2     0     0
 0     0     0     0     0     0     1     1     2     2     0     0
 0     0     0     0     0     0     0     0     1     1     2     2
 0     0     0     0     0     0     0     0     1     1     2     2

答案 2 :(得分:0)

在两个对角线中创建一个包含数字ab的矩阵,然后使用kron在块中重复这些数字:

m = 2; %// size of P0, P1
n = 4; %// number of blocks
a = 5;
b = 10;

h = diag(a*ones(1,n)) + diag(b*ones(1,n-1),1);
h(end,end+1) = b;
H = kron(h,ones(m));

结果:

H =
     5     5    10    10     0     0     0     0     0     0
     5     5    10    10     0     0     0     0     0     0
     0     0     5     5    10    10     0     0     0     0
     0     0     5     5    10    10     0     0     0     0
     0     0     0     0     5     5    10    10     0     0
     0     0     0     0     5     5    10    10     0     0
     0     0     0     0     0     0     5     5    10    10
     0     0     0     0     0     0     5     5    10    10