1

I am looking for an efficient implementation of the following operation in MATLAB.

Assume that there is A = blkdiag(A_1,...,A_i,...,A_n) and B = blkdiag(B_1,...,B_i,...,B_n). Assume there is some function of two blocks f(A_i,B_i) and output variable F = blkdiag(f(A_1,B_1),...,f(A_i,B_i),...,f(A_n,B_n)). Therefore, I am asking your help about how to accomplish this task without using loops.

In my case f(A_i,B_i) = U'*kron(A_i,B_i)*U'for some matrix U and n is about 100. Blocks A_i and B_i are of equal size for all i. All help will be appreciated.

  • Are `A_1`... `A_i` etc really separate variables? Or are they stored in an array somehow? – EdR May 22 '18 at 06:26
  • 2
    Well, you can store them in 3-dimensional array, such as `A(:,:,i) = A_i`. `A` is generated as `for a = 1:n a= rand(m); A(:,:,i) = a*a' end` – Mykola Servetnyk May 22 '18 at 06:42
  • How large is each block? and why would you like to avoid for-loops? – Nicky Mattsson May 22 '18 at 13:32
  • @NickyMattsson , my block size is quite small, m=4. I would like to avoid loops because each iteration is independent of others, hence I see potential to speed up my implementation. It is similar to multiplication of block-diagonal matrices, you can either multiply them at once(and matlab will do efficient multiplication) or do the loop multiplying each block. Theoretically it is the same, but practically can be a computational time gain. – Mykola Servetnyk May 22 '18 at 13:45
  • 4
    I think your question could benefit from an explanation of the broader picture of what it is that you're trying to do. Perhaps [the implementation that you have in mind](https://meta.stackexchange.com/questions/66377) is not the optimal one, in which case somebody might suggest a better one, or a built-in function that could help. – Dev-iL May 22 '18 at 14:43
  • 1
    There is no such thing a “all at once” in a computer, vectorised code is also a for-loop, although it is hidden. It is true that vectorised code is optimised, but this is most often observed with binary singular functions, which yours is not. In more recent versions of MATLAB the for-loop have been severely optimised with the introduction of the JIT compiler. Lastly that the blocks are independent does not imply vectorisation, it implies parallelisation, which you can also obtain with parfor, though I doubt it will be faster with only 100 blocks of size 4x4. – Nicky Mattsson May 22 '18 at 17:09

0 Answers0