Block diagonal matrix of identity times scalar.
2 ビュー (過去 30 日間)
古いコメントを表示
Mohammed Kagalwala
2019 年 11 月 29 日
編集済み: Philippe Lebel
2019 年 11 月 29 日
Hi,
I currently have a vector a = [1 2]', I wish to create a block diagonal matrix. Each block is identity (3x3) times the corresponding scalar in the vector a.
i.e. with a = [1 2]' I want to produce b = [1 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 0 0 0; 0 0 0 2 0 0; 0 0 0 0 2 0; 0 0 0 0 0 2]. The catch is a can be a vector of N x 1, thus b is of size 3*N x 3*N
The answer also has to be for loop free. I've tried using blkdiag() and eye() but have dimension issues in my multiplication
0 件のコメント
採用された回答
Philippe Lebel
2019 年 11 月 29 日
編集済み: Philippe Lebel
2019 年 11 月 29 日
here is my take.
a=[1,2];
c = round(a(1):1/(length(b(:,1))-1):a(2));
matrix = diag(c);
3 件のコメント
Philippe Lebel
2019 年 11 月 29 日
編集済み: Philippe Lebel
2019 年 11 月 29 日
here it is:
clear
a=[1,2,3,4];
size_of_sub_matrices = 2;
a = arrayfun(@(x) ones(1,size_of_sub_matrices)*x, a, 'UniformOutput', false);
a = cell2mat(a);
matrix = diag(a)
matrix =
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 2 0 0 0 0
0 0 0 0 3 0 0 0
0 0 0 0 0 3 0 0
0 0 0 0 0 0 4 0
0 0 0 0 0 0 0 4
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!