help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Building a multidimensional block diagonal matrix avoiding loops


From: Jose
Subject: Building a multidimensional block diagonal matrix avoiding loops
Date: Fri, 12 Apr 2013 21:43:20 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]