Simple way to create a block-diagonal matrix from a single vector.
56 views (last 30 days)
Show older comments
I am working with a vector of the following form
and I would like to transform this vector into a block-diagonal matrix
without resorting to any sort of looping.
If I call the above vector x the following does, in principle, work
Z = reshape(x',3,4)';
[eye(2),zeros(2),zeros(2,4);zeros(2,4),zeros(2),eye(2)]*blkdiag(Z,Z)
It's just that my actual vector is fairly high dimensional and I feel that there must be a more organic way to obtain the same result, i.e., a way that does not rely on the manual construction of a selection matrix and that does not require typing
blkdiag(Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,Z,..)
everytime the dimensionality changes. So essentialy, I would like to take a vector populate the first n coordinates of the first row of a matrix with the first n coodinates of this vector, fill in the rest of this row with zeros, move to the second row, populate the first first n coordinates with the second (!) n coordinates coordinates of my vector and continue until I reached the -st coordinate of my vectore, then I start populating the first n cooridnates of the -st row of my matrix with zeros and the second n cooridnates with the next elements of my vector, and so on...
I'd very much appreciate any pointers.
Thanks!
0 Comments
Accepted Answer
Star Strider
on 28 Nov 2021
I don’t understand exactly what ‘z’ is or what the desired result is.
One possibility is to replicate the original matrix using repmat, then create it as a cell array using mat2cell, and then providing that result as the argument to blkdiag.
If I understand the problem correctly, this approach appears to provide the automation desired —
d = 3; % Matrix Dimension (For This Simulation)
n = 5; % Desired Number Of Repititions
z = randi([10 99],d) % Matrix To Be Replicated
zm = repmat(z,1,n)
zmc = mat2cell(repmat(z,1,n), d, ones(1,n)*d)
zmc{1} % Demonstrate That The Result Is As Desired
zmc{end} % Demonstrate That The Result Is As Desired
BD = blkdiag(zmc{:}) % Show Final Result
This replicates the original matrix, segments the replicated matrix into a cell array (a comma-separated list, exactly what blkdiag wants for an argument), then creates the bolck diagonal matrix.
.
2 Comments
Star Strider
on 28 Nov 2021
As always, my pleasure!
This was an interesting (and challenging) problem!
.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!