- #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) );
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: