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

Click For Summary
SUMMARY

The discussion focuses on using MATLAB functions triu and tril to manipulate symmetric matrices by copying elements from the upper triangle to the lower triangle and vice versa. The user seeks assistance with understanding a provided code snippet that utilizes matrix indexing to achieve this transformation but encounters a mupadmex error during execution. Additionally, they face a CAT error when attempting to build the half-matrix, indicating issues with empty components in the matrix.

PREREQUISITES
  • Familiarity with MATLAB matrix operations
  • Understanding of symmetric matrices
  • Knowledge of MATLAB indexing techniques
  • Experience with error handling in MATLAB
NEXT STEPS
  • Research the usage of triu and tril functions in MATLAB
  • Learn about MATLAB's matrix indexing and manipulation techniques
  • Explore solutions for handling mupadmex and CAT errors in MATLAB
  • Investigate methods for constructing half-matrices in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working with matrix operations, symmetric matrices, and error troubleshooting in their code.

iqjump123
Messages
58
Reaction score
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
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
2K