1

blkdiag uses varargin to construct a block diagonal matrix from the input arguments (each matrix which needs to be inserted into the diagonal).

out = blkdiag(a,b,c,d,...), where a, b, c, d, ... are matrices, outputs a block diagonal matrix of the form

enter image description here

Suppose I had a cell array of matrices (or some other data structure) ; how would I feed this to a function like blkdiag?

5
  • Damn. Just figured out the answer. Ah well ... Commented Oct 31, 2011 at 19:29
  • @Phonon: That's generally not good form :) Commented Oct 31, 2011 at 19:42
  • It does however give good information to people looking for help on SO. Commented Oct 31, 2011 at 19:45
  • 1
    @Jacob: I disagree. I think it's perfectly ok to post a good answer to your own question. Commented Oct 31, 2011 at 19:53
  • @Jonas: Yep, definitely, but I personally prefer to do it if my answer is better/remains unanswered. Commented Oct 31, 2011 at 23:17

1 Answer 1

3

Use {:} to expand it for the function:

blocks = cell(2);
blocks{1} = rand(2);
blocks{2} = rand(2);
out = blkdiag(blocks{:})

Answer:

out =

0.6787    0.7431         0         0
0.7577    0.3922         0         0
     0         0    0.6555    0.7060
     0         0    0.1712    0.0318
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.