필터 지우기
필터 지우기

How to create a symmetrical matrix?

조회 수: 128 (최근 30일)
ric1321
ric1321 2017년 2월 3일
편집: Stephen23 2023년 1월 27일
Hi, I have to fill a matrix with some data that come from a subroutine. The matrix is symmetrical over the two diagonals, so I have to build just a quarter of it and then to copy one the other parts. How can I do?
  댓글 수: 1
Stephen23
Stephen23 2017년 2월 3일
편집: Stephen23 2017년 2월 3일
@ric1321: what happens along the diagonals? Are the diagonal values constant?

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

채택된 답변

Stephen23
Stephen23 2017년 2월 3일
편집: Stephen23 2017년 2월 3일
I looked at this task, and tried several possibilities to convert this:
>> M = [1,2,3,4,5;0,6,7,8,0;0,0,9,0,0;0,0,0,0,0;0,0,0,0,0]
M =
1 2 3 4 5
0 6 7 8 0
0 0 9 0 0
0 0 0 0 0
0 0 0 0 0
But then it occurred to me: the question states "I have to fill a matrix with some data that come from a subroutine", so perhaps the simplest way is to avoid it all together: if you have to fill the matrix with these values (presumably in a loop and using some indexing), then instead of just filling one value, fill in all four values at once. This is likely going to be simpler than any method of taking that quarter matrix, rotating, and merging again...
So the simplest way for your situation might be to not "copy and rotate", but to allocate each new value to four locations when you put it into the matrix:
S = size(M);
[R,C] = find(M);
Z = nan(S);
for k = 1:numel(R) % for each subroutine value
x = [R(k),1+S(1)-R(k),C(k),1+S(2)-C(k)]; % index
m = M(R(k),C(k)); % new subroutine value here
Z(x,x) = m;
end
which gives this:
Z =
5 4 3 4 5
4 8 7 8 4
3 7 9 7 3
4 8 7 8 4
5 4 3 4 5
Or you can use the method exactly as I did here: to copy the elements of that quarter to the other quarters.

추가 답변 (2개)

Guillaume
Guillaume 2017년 2월 3일
편집: Guillaume 2017년 2월 3일
One of the many ways to do this:
v = [1 2 3 4; 0 5 6 7; 0 0 8 9; 0 0 0 10];
tv = v';
v(tril(true(size(v)))) = tv(tril(true(size(v))))

Benjamin Klassen
Benjamin Klassen 2023년 1월 26일
Symmetric Matrix Generator
% Rows
r1=[1,2,3,4,5,6,7,8];
r2=[5,2,6,7,9,8,5];
r3=[1,4,6,2,8,6];
r4=[1,6,2,9,4];
r5=[1,3,2,7];
r6=[1,7,9];
r7=[6,2];
r8=[9];
nr=length(r1); % Number of rows
K=zeros(nr,nr);
% Symmetric Matrix Assembly
for i=1:nr
d=join(['r',num2str(i)])
rowvalues=eval(d);
K(i,i:end)=rowvalues;
K(i:end,i)=rowvalues;
end
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 27일
편집: Stephen23 2023년 1월 27일
This would be much better without the evil EVAL call and anti-pattern numbered variable names:
C = {[1,2,3,4,5,6,7,8];[5,2,6,7,9,8,5];[1,4,6,2,8,6];[1,6,2,9,4];[1,3,2,7];[1,7,9];[6,2];9}
C = 8×1 cell array
{[1 2 3 4 5 6 7 8]} {[ 5 2 6 7 9 8 5]} {[ 1 4 6 2 8 6]} {[ 1 6 2 9 4]} {[ 1 3 2 7]} {[ 1 7 9]} {[ 6 2]} {[ 9]}
N = numel(C);
M = zeros(N,N);
for k = 1:N
V = C{k};
M(k,k:end) = V;
M(k:end,k) = V;
end
display(M)
M = 8×8
1 2 3 4 5 6 7 8 2 5 2 6 7 9 8 5 3 2 1 4 6 2 8 6 4 6 4 1 6 2 9 4 5 7 6 6 1 3 2 7 6 9 2 2 3 1 7 9 7 8 8 9 2 7 6 2 8 5 6 4 7 9 2 9

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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by