Jacob
Jacob

Reputation: 34601

Passing variables to functions with variable length input arguments

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?

Upvotes: 1

Views: 728

Answers (1)

Bee
Bee

Reputation: 2502

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

Upvotes: 3

Related Questions