Clear Filters
Clear Filters

Create parametric block diagonal matrix

2 views (last 30 days)
how can create parametric block diagonal matrix in Matlab
for i=1:2
H = blkdiag(sym('H%d',num2str(i),[1 1]));
end

Answers (1)

Star Strider
Star Strider on 15 Feb 2017
See if this does what you want:
h = sym('H%d',[1 2]); % Create Vector
h = mat2cell(h, 1, [1 1]); % Convert Vector To Cell Array Of Individual Elements (Comma-Separated List)
H = blkdiag(h{:}) % Desired Result
H =
[ H1, 0]
[ 0, H2]
The blkdiag function wants a comma-separated list, and a cell array provides that. This code creates your initial vector (as I believe you want it), then converts it to a cell array, then uses it to create your block-diagonal matrix.
If this is not the result you want, please describe in detail what you want your desired result to be.
  2 Comments
Mehdi Ghasem Moghadam
Mehdi Ghasem Moghadam on 15 Feb 2017
Thanks, but I need Block Diagonal matrix like: H = [ H1, 0] [ 0, H2] [ 0, H3]
Star Strider
Star Strider on 15 Feb 2017
This seems to do what you want:
h = sym('H%d',[1 3]); % Create Vector
h = mat2cell(h, 1, [1 2]); % Convert To Cell Array
H = blkdiag(h{:}).' % Desired Result (Simple — Not Complex Conjugate — Transpose)
H =
[ H1, 0]
[ 0, H2]
[ 0, H3]

Sign in to comment.

Categories

Find more on Operating on Diagonal 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!