필터 지우기
필터 지우기

Exploiting symmetry in multidimensional arrays

조회 수: 2 (최근 30일)
Patrick Mboma
Patrick Mboma 2014년 7월 13일
댓글: Roger Stafford 2014년 7월 14일
Dear all, In a symmetric matrix A of size [n,n], for two indices i and j such that 1<=i<=n and 1<=j<=n we have that A(i,j)=A(j,i). Suppose I know A(i,j) for i>=j how can I efficiently set A(j,i)?
More generally, if I have a hypercube A of size n^m, how can I, on the basis of A(i1,i2,...,im) with i1>=i2>=i3....>=im, set all A for all permutations of i1,i2,...,im instead of recalculating the entry for each permutation.
Thanks,

채택된 답변

Roger Stafford
Roger Stafford 2014년 7월 13일
For a general m-dimensional n^m array, A, do this:
[I1,I2,...,Im] = ndgrid(1:n);
P = [I1(:),I2(:),...,Im(:)];
Q = sort(P,2,'descend');
A(sub2ind(size(P),P(:,1),P(:,2),...,P(:,m))) = ...
A(sub2ind(size(Q),Q(:,1),Q(:,2),...,Q(:,m)))
Note: The four three-dot ellipses (...) shown above (but not the one following the '=' sign) are to be filled in by the user.
  댓글 수: 4
Patrick Mboma
Patrick Mboma 2014년 7월 14일
편집: Patrick Mboma 2014년 7월 14일
@Alfonso What you suggest would really be neat and seems to work well in the two-dimensional case that I tested. I still need to investigate higher dimensions and the speed.
Roger Stafford
Roger Stafford 2014년 7월 14일
@Patrick. Yes, you are quite right. I should have used size(A). I must have had a mental black out at that point! :-)

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 7월 13일
For a 2D array it's easy:
B = tril(A,-1)
A = B+B.'+diag(A);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by