use array vector to form a diagonal matrix

2 ビュー (過去 30 日間)
SYED SHAZAIB
SYED SHAZAIB 2023 年 1 月 1 日
編集済み: DGM 2023 年 1 月 1 日
For diag() function(https://ww2.mathworks.cn/help/matlab/ref/diag.html) the vector 'v' is used to form a diagonal matrix, but 'v' have single elements( i.e. v = [2 1 -1 -2 -5]). I want to use 'v' which has {2x2} array matrices as it's elements to creat a diagonal matrix i.e. [{2x2} {2x2} {2x2} {2x2} {2x2}].
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 1 月 1 日
What would be the output for
v = [1 2;3 4]
v = 2×2
1 2 3 4

サインインしてコメントする。

回答 (1 件)

DGM
DGM 2023 年 1 月 1 日
編集済み: DGM 2023 年 1 月 1 日
It seems to me like maybe you're talking about what blkdiag() does.
A = [1 2; 3 4];
B = A*11;
C = A*111;
D = blkdiag(A,B,C) % block-diagonal concatenation
D = 6×6
1 2 0 0 0 0 3 4 0 0 0 0 0 0 11 22 0 0 0 0 33 44 0 0 0 0 0 0 111 222 0 0 0 0 333 444
If your input v is some sort of cell array, you'll have to manage that, but it's not really clear to me how you have that arranged. Say you have a cell vector of numeric matrices:
v = {A B C}; % same thing, but in a cell
D = blkdiag(v{:}) % same thing
D = 6×6
1 2 0 0 0 0 3 4 0 0 0 0 0 0 11 22 0 0 0 0 33 44 0 0 0 0 0 0 111 222 0 0 0 0 333 444

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by