[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Building a multidimensional block diagonal matrix avoiding loops
From: |
c. |
Subject: |
Re: Building a multidimensional block diagonal matrix avoiding loops |
Date: |
Fri, 12 Apr 2013 20:47:41 +0200 |
On 12 Apr 2013, at 20:43, Jose <address@hidden> wrote:
> Hello.
>
> I need to create a multidimensional block diagonal matrix from another
> multidimensional matrix. Let me put a simplified example.
>
> I have A
> A(:,:,1)=[1 2;3 4];
> A(:,:,2)=[5 6;7 8];
>
> from which I want to create B
> B(:,:,1)=[1 2 0 0;3 4 0 0];
> B(:,:,2)=[0 0 5 6;0 0 7 8];
>
> so the expansion occurs in diagonal blocks in the second dimension.
>
> If I had the problem with a 2D matrix, I could combine mat2cell and blkdiag
> to create the new expanded matrix. But having a multidimensional matrix I
> cannot see a way to avoid a loop, such as for example:
>
> [l,m,n]=size(A);
> for k=1:n
> B(:,:,k)=[zeros(l,m*(k-1)) A(:,:,k) zeros(l,m*(n-k))];
> endfor
>
> Anybody has any idea about how to make this avoiding the loop?
>
> BR
> Jose
I think the function "kron" could be of help with your problem.
HTH,
c.