how to solve diagonal matrix of each page of a 3D vector ? pagediag?
7 ビュー (過去 30 日間)
古いコメントを表示
There is a 3D vector A, it has 100 pages, each page is a 1×20 vector. I want to solve diagonal matrix of each 1×20 vector.
A=rand(1,20,100);
for k=1:100
B(:,:,k)=diag(A(:,:,k));
end
B is a 20×20×100 matrix.
I'm trying to vectorize this expression, is there a matlab function maybe called 'pagediag' that can solve B=pagediag(A) ?
1 件のコメント
Matt J
2023 年 6 月 28 日
I am concerned about why you think you need a pagediag.If you plan to use it in conjunction with pagemtimes to scale the rows or columns of a stack of matrices, it's the wrong approach.
回答 (3 件)
Bruno Luong
2023 年 6 月 28 日
[~,n,p] = size(A);
[J,K] = ndgrid(1:n,1:p);
B = accumarray([J(:) J(:) K(:)], A(:), [n,n,p]);
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!