Adding mirror image of lower triangle of matrix to upper half of matrix

49 ビュー (過去 30 日間)
Samuel
Samuel 2011 年 10 月 10 日
編集済み: Witold Waldman 2022 年 4 月 12 日
Hello all!
I just had a question about combining elements of matrices. In the matlab documentation, there was a function called triu and tril that extracts the upper and lower components of a matrix, respectively. I was wondering if there was a way to copy the elements of the upper triangle to the lower triangle portion of the symmetric matrix (or visa versa) as a mirror image to one another?
EG-
haha =
1 0 0 ;
1 1 0;
1 0 1 ;->
function (copy lower half to upper half)(haha)
1 1 1;
1 1 0;
1 0 1;
any help will be appreciated. thanks!
UPDATE- I found an article from a website that wrote the method below, but I can't entirely understand it, and moreover, don't know how I can apply this for the lower matrix, copying to the upper half. I was hoping to be able to understand the code to be able to convert, but I can't understand the code.. and testing the code gives me a mupadmex error.
here it is:
[ i j ] = find(tril(ones(m), 1)); %Trick to get indices.
D = zeros(m, m); %Initialise output matrix.
D( i + m*(j-1) )= sqrt(sum(abs( kmat(i,:) - kmat(j,:) ).^2, 2));
D( j + m*(i-1) )= D( i + m*(j-1) );
to add to this question.
When I actually try to go through the operation to build the first half triangle of the matrix (in order to copy it to the upper half), it gives me a CAT error, saying that a matrix cannot contain components that are empty. Is there a way to build a half-matrix so I can go through this entire operation without having to manually pad the other half of the matrix with zeros?
Thanks!
samuel

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 10 月 10 日
tril(haha,-1)'+haha
ADD
h1 =[ 1 1 0 1 0
0 0 1 0 1
0 1 0 1 0
0 0 0 0 0
0 0 0 1 0]
triu(h1)+triu(h1,1)' % upper matrix, copyed to the lower half
tril(h1)+tril(h1,-1)' % lower matrix, copyed to the upper half

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2011 年 10 月 10 日
The only way to build a "half-matrix" is to use cell arrays, which are usually a nuisance for this kind of work.
Here is a trick for square matrices, provided that only half the matrix (together with the diagonal) are occupied:
B = haha + haha.';
B(1:size(B,1)+1:end) = diag(haha);

Quincy van den Berg
Quincy van den Berg 2012 年 3 月 27 日
The trick mentioned above seems to work, adding the transpose to your original matrix. It does however double the diagonal so it needs to be removed.
Suppose you have a matrix H that you want to mirror the upper half of:
H=H+H'-diag([diag(H)]);

Witold Waldman
Witold Waldman 2022 年 4 月 12 日
編集済み: Witold Waldman 2022 年 4 月 12 日
Copying (not adding) the mirror image of a lower triangle of a lower triangular matrix to the upper half of the matrix can be done with the following code:
% A is a 3x3 complex lower triangular matrix.
A = [1,0,0; 2+2*1i,3,0; 4,5,6+6*1i]
A = A+transpose(tril(A,-1));
A
The use of the transpose() function, instead of the ' complex conjugate transpose operator, is necessary to ensure that complex matrices are treated correctly, and do not have their elements transformed into complex conjugate values.
Note that the approach given above relies on matrix addition, which for the copy requirement is unnecessary. A faster, and potentially clearer, bit of code, which works for real as well as complex matrices, would be:
% A is a 3x3 complex lower triangular matrix.
A = [1,0,0; 2+2*1i,3,0; 4,5,6+6*1i]
for i = 2:size(A,1), A(1:i,i) = A(i,1:i); end;
A
Here, we simply step through the relevant portions of the lower triangular part of the matrix, selecting the required elements in a column, and copying them to the appropriate elements in the equivalent row. This approach of copying, rather than addition, may prove much quicker when dealing with large matrices.

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by