indexing diagonals out of a 4d matrix

조회 수: 2 (최근 30일)
Neekar Mohammed
Neekar Mohammed 2019년 6월 23일
댓글: Matt J 2019년 6월 25일
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
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.
Neekar Mohammed
Neekar Mohammed 2019년 6월 24일
Thank you very much for your comments. I have a correlation data which is a 4D matrix, let's say g(x1,y1,x2,y2) I want to calculate the intensity which is a 2D matrix f(x,y)=g where x1=x2 and y1=y2. So yes I want it to become a 2D matrix. Many thanks. Neekar

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
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
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
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by