MATLAB: Create a block diagonal matrix with same repeating block

前端 未结 5 1416
独厮守ぢ
独厮守ぢ 2020-12-09 11:03

I have a matrix K of dimensions n x n. I want to create a new block diagonal matrix M of dimensions N x N, such that it c

相关标签:
5条回答
  • 2020-12-09 11:16
    tmp = repmat({K},d,1);
    M = blkdiag(tmp{:});
    

    You should never use eval, or go into for loops unnecessarily. Kron is a very elegant way. Just wanted to share this as it also works.

    0 讨论(0)
  • 2020-12-09 11:19
    s = 'A,';
    s = repmat(s,[1,n2]);
    s = ['B=blkdiag(', s(1:end-1),');'];
    eval(s);
    

    It can be faster than using kron-eye.

    0 讨论(0)
  • 2020-12-09 11:29

    A "for" loop may might help. Like:

    M = k;
    for i=1:N/n - 1
        M=blkdiag(M,k);
    end
    
    0 讨论(0)
  • 2020-12-09 11:35

    The following should work:

    d=5; K=eye(3); T = cell(1,d);

    for j=1:d T{j} =K; end

    M = blkdiag(T{:})

    0 讨论(0)
  • 2020-12-09 11:39

    you can use kron for that.

    M = kron(X,Y)
    

    returns the Kronecker tensor product of X and Y. The result is a large array formed by taking all possible products between the elements of X and those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is m*p-by-n*q. So in your case something like this will do:

    M = kron(eye(L),K)
    

    with L the # of blocks.

    0 讨论(0)
提交回复
热议问题