在for循环中构建对角线

时间:2016-02-06 18:41:31

标签: matlab diagonal

尝试对角地附加到mysql_fetch_assoc循环中的矩阵:

for

显然这不起作用,因为我重写了这个值。我想构建一个由for ii=1:10 R1 = [1,2;3,4]; Matrix is always 2x2 but different values each iteration cov = blkdiag(R1); end 值组成的矩阵,例如

R1

如果可以在[ R1,0,0,0..., 0,R1,0,0...] 循环

中完成,我可以使用其他技术来实现最终目标

2 个答案:

答案 0 :(得分:2)

只要我们循环和增长矩阵,这个怎么样?

for ii = 1:10
   R1 = [1 2; 3 4];   %// placeholder for function that generates R1
                      %// move this line and next before loop if R1 is static
   [m,n] = size(R1);
   cov(end+1:end+m,end+1:end+n) = R1;
end

答案 1 :(得分:-1)

R1 = [1,2;3,4];                              %// initial R1 matrix
CurrentOut = R1;                             %// initialise output
for ii = 1:10
    R1 = [1,2;3,4];                          %// placeholder for function that generates R1
    [A,B] = size(CurrentOut);                %// get current size
    tmpout(A+2,B+2)=0;                       %// extend current size
    tmpout(1:A,1:B) = CurrentOut;            %// copy current matrix
    tmpout(A+1:end,B+1:end) = R1;            %// add additional element
    CurrentOut=tmpout;                       %// update original
end

如你所说,for循环不是最好的方法,但确实可以做到。