在 for 循环中构建对角线
Build Diagonal in for loop
尝试在 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...]
我可以使用其他技术实现最终目标,只是好奇它是否可以在 for
循环中完成
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
循环不是执行此操作的最佳方法,但确实可以做到。
只要我们循环和增长矩阵,这个怎么样?
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
尝试在 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...]
我可以使用其他技术实现最终目标,只是好奇它是否可以在 for
循环中完成
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
循环不是执行此操作的最佳方法,但确实可以做到。
只要我们循环和增长矩阵,这个怎么样?
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