triangular

How to efficiently generate lower triangle indices of a symmetric matrix

偶尔善良 提交于 2020-01-02 06:29:50
问题 I need to generate lower triangle matrix indices (row and columns pairs). The current implementation is inefficient (memory wise) specially when symmetric matrix gets big (more than 50K rows). Is there a better way? rows <- 2e+01 id <- which(lower.tri(matrix(, rows, rows)) == TRUE, arr.ind=T) head(id) # row col # [1,] 2 1 # [2,] 3 1 # [3,] 4 1 # [4,] 5 1 # [5,] 6 1 # [6,] 7 1 回答1: Here's another approach: z <- sequence(rows) cbind( row = unlist(lapply(2:rows, function(x) x:rows), use.names =

Reading a symmetric matrix from file that omits upper triangular part

一个人想着一个人 提交于 2019-12-21 06:24:30
问题 Using R, what is the best way to read a symmetric matrix from a file that omits the upper triangular part. For example, 1.000 .505 1.000 .569 .422 1.000 .602 .467 .926 1.000 .621 .482 .877 .874 1.000 .603 .450 .878 .894 .937 1.000 I have tried read.table , but haven't been successful. 回答1: Here's a read.table and loopless and *apply-less solution: txt <- "1.000 .505 1.000 .569 .422 1.000 .602 .467 .926 1.000 .621 .482 .877 .874 1.000 .603 .450 .878 .894 .937 1.000" # Could use clipboard or

Linear index upper triangular matrix

ぐ巨炮叔叔 提交于 2019-12-17 17:27:10
问题 If I have the upper triangular portion of a matrix, offset above the diagonal, stored as a linear array, how can the (i,j) indices of a matrix element be extracted from the linear index of the array? For example, the linear array [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 is storage for the matrix 0 a0 a1 a2 a3 0 0 a4 a5 a6 0 0 0 a7 a8 0 0 0 0 a9 0 0 0 0 0 And we want to know the (i,j) index in the array corresponding to an offset in the linear matrix, without recursion. A suitable result, k2ij

How can I create a triangular matrix based on a vector, in MATLAB?

让人想犯罪 __ 提交于 2019-12-12 08:23:33
问题 Let's say I've got a vector like this one: A = [101:105] Which is really: [ 101, 102, 103, 104, 105 ] And I'd like to use only vector/matrix functions and operators to produces the matrix: 101 102 103 104 105 102 103 104 105 0 103 104 105 0 0 104 105 0 0 0 105 0 0 0 0 or the following matrix: 101 102 103 104 105 0 101 102 103 104 0 0 101 102 103 0 0 0 101 102 0 0 0 0 101 Any ideas anyone? (I'm very much a novice in MATLAB, but I've been saddled this stuff...) 回答1: hankel(A) will get you the

Making grid triangular mesh quickly with Numpy

ぃ、小莉子 提交于 2019-12-10 10:36:47
问题 Consider a regular matrix that represents nodes numbered as shown in the figure: I want to make a list with all the triangles represented in the figure. Which would result in the following 2 dimensional list: [[0,1,4],[1,5,4],[1,2,5],[2,6,5],...,[11,15,14]] Assuming that the dimensions of the matrix are ( Nr X Nc ) ((4X4) in this case), I was able to achieve this result with the following code: def MakeFaces(Nr,Nc): Nfaces=(Nr-1)*(Nc-1)*2 Faces=np.zeros((Nfaces,3),dtype=np.int32) for r in

How do you find which row and column a number belongs to in Floyd Triangle

时光毁灭记忆、已成空白 提交于 2019-12-08 10:44:00
问题 How do you find which row and column does a number belongs to in Floyd Triangle? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 For example, 33 is in the 8th row and 5th column (input 33 → output 8th row, 5th column) 46 is in the 10th row and 1st column 27 is in the 7th row and 6th column Thank you so much in advance! 回答1: Note that n-th row ends with value n*(n+1)/2 . So you can make

Making grid triangular mesh quickly with Numpy

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 04:51:46
Consider a regular matrix that represents nodes numbered as shown in the figure: I want to make a list with all the triangles represented in the figure. Which would result in the following 2 dimensional list: [[0,1,4],[1,5,4],[1,2,5],[2,6,5],...,[11,15,14]] Assuming that the dimensions of the matrix are ( Nr X Nc ) ((4X4) in this case), I was able to achieve this result with the following code: def MakeFaces(Nr,Nc): Nfaces=(Nr-1)*(Nc-1)*2 Faces=np.zeros((Nfaces,3),dtype=np.int32) for r in range(Nr-1): for c in range(Nc-1): fi=(r*(Nc-1)+c)*2 l1=r*Nc+c l2=l1+1 l3=l1+Nc l4=l3+1 Faces[fi]=[l1,l2

Reading a symmetric matrix from file that omits upper triangular part

老子叫甜甜 提交于 2019-12-03 20:36:46
Using R, what is the best way to read a symmetric matrix from a file that omits the upper triangular part. For example, 1.000 .505 1.000 .569 .422 1.000 .602 .467 .926 1.000 .621 .482 .877 .874 1.000 .603 .450 .878 .894 .937 1.000 I have tried read.table , but haven't been successful. Here's a read.table and loopless and *apply-less solution: txt <- "1.000 .505 1.000 .569 .422 1.000 .602 .467 .926 1.000 .621 .482 .877 .874 1.000 .603 .450 .878 .894 .937 1.000" # Could use clipboard or read this from a file as well. mat <- data.matrix( read.table(text=txt, fill=TRUE, col.names=paste("V", 1:6))

Store triangular matrix efficiently

我的未来我决定 提交于 2019-12-03 12:49:55
I need to efficiently store a lower triangular matrix by not storing all the zeroes in the memory, so I have thought about it this way: first I allocate memory for every row, then for each row I allocate i+1 bytes, so I never have to worry about the zeroes, but something is wrong at the first allocation. What am I doing wrong? This is my code, and the compiler exits the program at line 8, just after reading the dimension of the matrix. #include <stdio.h> #include <stdlib.h> int main () { int i, j, **mat1, dim; scanf("%d",&dim); *mat1 = (int**)calloc(dim, sizeof(int*)); for(i = 0; i<dim; i++)