indexing diagonals out of a 4d matrix
2 ビュー (過去 30 日間)
古いコメントを表示
I would like to extract diag elemnts of a 4d matrix and put them in 2d matrix. Any help would be much appreciated.
5 件のコメント
John D'Errico
2019 年 6 月 24 日
Give an example, as the diagonal of a 4-d matrix has no definition. If anything, as Bjorn points out, the result would be ONE dimensional, a vector. So wanting it to become a 2-d matrix makes little sense. That means you need to be clear and specific as to your intent.
採用された回答
Matt J
2019 年 6 月 25 日
編集済み: Matt J
2019 年 6 月 25 日
[M1,N1,M2,N2]=size(g);
M=min(M1,M2); N=min(N1,N2);
f=diag(reshape(g(1:M,1:N,1:M,1:N),M*N,[]));
f=reshape(f,M,N);
2 件のコメント
Matt J
2019 年 6 月 25 日
Nekar's comment moved here:
Thank you very much for fast respond. If is like g(x1,x2,y1,y2) (I changed the order) then f(x,y)=g for x1=x2 and y1=y2, Would be the same answer you suggested?
Matt J
2019 年 6 月 25 日
No, you would have to pre-permute g into (x1,y1,x2,y2) ordering for it to work
g=permute(g,[1,3,2,4]);
For this reason and others, I recommend that you do not use (x1,x2,y1,y2) ordering. It will necessitate a lot of extra manipulation.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!