[Matlab]Copy Lower Triangle of symmetric matrix to Upper Triangle(or visa versa)

In summary, the conversation discussed a question about combining elements of matrices in MATLAB. The function triu and tril were mentioned as ways to extract the upper and lower components of a matrix. The question was about finding a method to copy the elements of the upper triangle to the lower triangle of a symmetric matrix (or vice versa). A suggested method from a website was provided, but there were difficulties in understanding and applying the code. The conversation also mentioned an error when trying to build the first half of the matrix, and a question about manually padding the other half with zeros.
  • #1
iqjump123
61
0
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)?

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) );
 
Last edited:
Physics news on Phys.org
  • #2
Some else 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!
 

1. How do I copy the lower triangle of a symmetric matrix to the upper triangle in Matlab?

To copy the lower triangle of a symmetric matrix to the upper triangle in Matlab, you can use the built-in function "triu". This function extracts the upper triangular part of a matrix and sets the lower triangular elements to zero. You can then add this upper triangular matrix to the original matrix to fill in the upper triangle.

2. Can I copy the upper triangle of a symmetric matrix to the lower triangle in Matlab?

Yes, you can use the built-in function "tril" to extract the lower triangular part of a matrix and set the upper triangular elements to zero. This lower triangular matrix can then be added to the original matrix to fill in the lower triangle.

3. What if my symmetric matrix has non-zero values in the diagonal?

If your symmetric matrix has non-zero values in the diagonal, you can use the "triu" and "tril" functions with an additional argument to specify the diagonal offset. For example, "triu(A,1)" will extract the upper triangular part of matrix A, excluding the main diagonal.

4. Is there a faster way to copy the lower triangle to the upper triangle in Matlab?

Yes, you can also use the "flipud" function to flip the lower triangle of the matrix and then add it to the original matrix. This method can be faster for larger matrices compared to using the "triu" function.

5. Can I copy the lower triangle to the upper triangle without adding it to the original matrix?

Yes, you can use the "bsxfun" function to perform element-wise operations on matrices of different sizes. You can use this function to add the lower triangular matrix with its transposed version, which will result in a matrix with the desired upper triangular elements without changing the original matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Replies
1
Views
759
  • Linear and Abstract Algebra
Replies
1
Views
733
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
1K
Back
Top