Copy upper triangle to lower triangle in a python matrix
To do this in NumPy, without using a double loop, you can use tril_indices
. Note that depending on your matrix size, this may be slower that adding the transpose and subtracting the diagonal though perhaps this method is more readable.
>>> i_lower = np.tril_indices(n, -1)
>>> matrix[i_lower] = matrix.T[i_lower] # make the matrix symmetric
Be careful that you do not try to mix tril_indices
and triu_indices
as they both use row major indexing, i.e., this does not work:
>>> i_upper = np.triu_indices(n, 1)
>>> i_lower = np.tril_indices(n, -1)
>>> matrix[i_lower] = matrix[i_upper] # make the matrix symmetric
>>> np.allclose(matrix.T, matrix)
False
matrix - mirror lower triangle to upper triangle
You can do:
tm <- t(m)[,-nrow(m)] # or t(m[-nrow(m),])
m[upper.tri(m)] <- tm[upper.tri(tm, diag = TRUE)]
# > m
# v_0 v_2 v_3 v_3a v_3b
# v_2 1 1 2 3 4
# v_3 2 66 66 77 88
# v_3a 3 77 333 333 444
# v_3b 4 88 444 101 101
# v_4 5 99 555 202 99999
Transforming a list containing the elements below the diagonal of a matrix into a full matrix
A solution using numpy.triu_indices
and numpy.tril_indices
. I have guided each step with comments. The key is to first find the upper right indices, assign the value from the list, then make the matrix symmetric.
import numpy as np
n = 4
l = [1,0.1,0.6,0.4,1,0.1,0.2,1,0.7,1]
a = np.zeros((n,n)) # Initialize nxn matrix
triu = np.triu_indices(n) # Find upper right indices of a triangular nxn matrix
tril = np.tril_indices(n, -1) # Find lower left indices of a triangular nxn matrix
a[triu] = l # Assign list values to upper right matrix
a[tril] = a.T[tril] # Make the matrix symmetric
print(a)
Output
[[1. 0.1 0.6 0.4]
[0.1 1. 0.1 0.2]
[0.6 0.1 1. 0.7]
[0.4 0.2 0.7 1. ]]
Forcing upper triangle values to lower triangle values in matrix after melt
Use the fact that Var2
(column index) for lower triangle will be smaller than the Var1
(row index).
mm$value2 = sapply(1:NROW(mm), function(i){
if (mm$Var2[i] - mm$Var1[i] < 0){
mm$value[i] = mm$value[mm$Var1 == mm$Var2[i] & mm$Var2 == mm$Var1[i]]
}else{
mm$value[i]
}
})
mm
# Var1 Var2 value value2
#1 1 1 0 0
#2 2 1 -1 1
#3 3 1 -2 2
#4 4 1 -3 3
#5 1 2 1 1
#6 2 2 0 0
#7 3 2 -4 4
#8 4 2 -5 5
#9 1 3 2 2
#10 2 3 4 4
#11 3 3 0 0
#12 4 3 -6 6
#13 1 4 3 3
#14 2 4 5 5
#15 3 4 6 6
#16 4 4 0 0
Extract upper or lower triangular part of a numpy matrix
Try numpy.triu
(triangle-upper) and numpy.tril
(triangle-lower).
Code example:
np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 0, 8, 9],
[ 0, 0, 12]])
Conditionally replace matrix upper-triangle elements with lower-triangle
z <- matrix(c(0,1,2,0,0,1,0,0,0),nrow=3,ncol=3)
z[upper.tri(z) & t(z) == 1] = 1
works for me.
Note: your upperTriangel and lowerTriangle do not appear to be part of base. You might want to indicate which package they are from.
Extract lower triangle portion of a matrix
If you're OK with creating a lower triangular matrix as an intermediate step, you can use logical indexing and tril!
with an extra argument to get what you need.
julia> M = [1.0 0.751 0.734
0.751 1.0 0.948
0.734 0.948 1.0];
julia> v = M[tril!(trues(size(M)), -1)]
3-element Array{Float64, 1}:
0.751
0.734
0.948
The trues
call returns an array of M's shape filled with boolean true
values. tril!
then prunes this down to just the part of the matrix that we want. The second argument to tril! tells it which superdiagonal to start from, which we use here to avoid the values in the leading diagonal.
We use the result of that for indexing into M, and that returns an array with the required values.
Related Topics
Multiple Graphs Over Multiple Pages Using Ggplot
Correlation Corrplot Configuration
Convert a Matrix with Dimnames into a Long Format Data.Frame
Automatic Adjustment of Margins in Horizontal Bar Chart
Ctree() - How to Get the List of Splitting Conditions for Each Terminal Node
How to Subset Data.Frames Stored in a List
R View() Does Not Display All Columns of Data Frame
Error When I Try to Predict Class Probabilities in R - Caret
The Result of Rpart Is Just with 1 Root
How to Plot One Variable in Ggplot
How to Draw Two Half Circles in Ggplot in R