matrix

What is the fastest way to compute a sparse Gram matrix in Python?

a 夏天 提交于 2021-01-27 07:22:56
问题 A Gram matrix is a matrix of the structure X @ X.T which of course is symmetrical. When dealing with dense matrices, the numpy.dot product implementation is intelligent enough to recognize the self-multiplication to exploit the symmetry and thus speed up the computations (see this). However, no such effect can be observed when using scipy.sparse matrices: random.seed(0) X = random.randn(5,50) X[X < 1.5] = 0 X = scipy.sparse.csr_matrix(X) print(f'sparsity of X: {100 * (1 - X.count_nonzero() /

Average every N rows by column

扶醉桌前 提交于 2021-01-27 07:21:12
问题 I have four matrices that are 240x30. I need to calculate an average for every 15 rows and I need to do this by column. So, in the end I should have 30 columns with 16 values. So, for instance: myMatrix = randi(240,30) And this is what I have so far: averageBins = 15; meanByBinsMyMatrix = arrayfun(@(i) mean(myMatrix (i:i+averageBins-1)),1:averageBins:length(myMatrix )-averageBins+1)'; % the averaged vectormean() This seems to be working but I think it only does the job for the first column.

Complex matrix exponential in C++

一笑奈何 提交于 2021-01-27 06:52:24
问题 Is it actually possible to calculate the Matrix Exponential of a Complex Matrix in c / c++? I've managed to take the product of two complex matrices using blas functions from the GNU Science Library. for matC = matA * matB: gsl_blas_zgemm (CblasNoTrans, CblasNoTrans, GSL_COMPLEX_ONE, matA, matB, GSL_COMPLEX_ZERO, matC); And I've managed to get the matrix exponential of a matrix by using the undocumented gsl_linalg_exponential_ss(&m.matrix, &em.matrix, .01); But this doesn't seems to accept

How to construct a matrix containing 9 smaller matrix

陌路散爱 提交于 2021-01-27 06:43:13
问题 I have nine matrices whose dimension as (N by N) A1(i,j),A2(i,j),A3(i,j),A4(i,j),A5(i,j),A6(i,j),A7(i,j),A8(i,j),A9(i,j) Then I want to construct a larger matrix (3N by 3N) including these nine matrices as: A = [A1 A2 A3 A4 A5 A6 A7 A8 A9] In fortran, can I use the command line as do i=1,FN do j=1,FML A(i,j) = [A1(i,j),A2(i,j),A3(i,j);A4(i,j),A5(i,j),A6(i,j);A7(i,j),A8(i,j),A9(i,j)] end do end do 回答1: Just for fun, you can also make the large A matrix by using do-loops as do i = 1, N A( i, :

How to construct a matrix containing 9 smaller matrix

橙三吉。 提交于 2021-01-27 06:43:08
问题 I have nine matrices whose dimension as (N by N) A1(i,j),A2(i,j),A3(i,j),A4(i,j),A5(i,j),A6(i,j),A7(i,j),A8(i,j),A9(i,j) Then I want to construct a larger matrix (3N by 3N) including these nine matrices as: A = [A1 A2 A3 A4 A5 A6 A7 A8 A9] In fortran, can I use the command line as do i=1,FN do j=1,FML A(i,j) = [A1(i,j),A2(i,j),A3(i,j);A4(i,j),A5(i,j),A6(i,j);A7(i,j),A8(i,j),A9(i,j)] end do end do 回答1: Just for fun, you can also make the large A matrix by using do-loops as do i = 1, N A( i, :

counting N occurrences within a ceiling range of a matrix by-row

社会主义新天地 提交于 2021-01-27 06:05:30
问题 I would like to tally each time a value lies within a given range in a matrix by-row, and then sum these logical outcomes to derive a "measure of consistency" for each row. Reproducible example: m1 <- matrix(c(1,2,1,6,3,7,4,2,6,8,11,15), ncol=4, byrow = TRUE) # expected outcome, given a range of +/-1 either side exp.outcome<-matrix(c(TRUE,TRUE,TRUE,FALSE, TRUE,FALSE,TRUE,TRUE, FALSE,FALSE,FALSE,FALSE), ncol=4, byrow=TRUE) Above I've indicated the the expected outcome, in the case where each

counting N occurrences within a ceiling range of a matrix by-row

廉价感情. 提交于 2021-01-27 06:05:12
问题 I would like to tally each time a value lies within a given range in a matrix by-row, and then sum these logical outcomes to derive a "measure of consistency" for each row. Reproducible example: m1 <- matrix(c(1,2,1,6,3,7,4,2,6,8,11,15), ncol=4, byrow = TRUE) # expected outcome, given a range of +/-1 either side exp.outcome<-matrix(c(TRUE,TRUE,TRUE,FALSE, TRUE,FALSE,TRUE,TRUE, FALSE,FALSE,FALSE,FALSE), ncol=4, byrow=TRUE) Above I've indicated the the expected outcome, in the case where each

counting N occurrences within a ceiling range of a matrix by-row

放肆的年华 提交于 2021-01-27 06:04:08
问题 I would like to tally each time a value lies within a given range in a matrix by-row, and then sum these logical outcomes to derive a "measure of consistency" for each row. Reproducible example: m1 <- matrix(c(1,2,1,6,3,7,4,2,6,8,11,15), ncol=4, byrow = TRUE) # expected outcome, given a range of +/-1 either side exp.outcome<-matrix(c(TRUE,TRUE,TRUE,FALSE, TRUE,FALSE,TRUE,TRUE, FALSE,FALSE,FALSE,FALSE), ncol=4, byrow=TRUE) Above I've indicated the the expected outcome, in the case where each

Insert row at a specific location in matrix using R

与世无争的帅哥 提交于 2021-01-27 05:12:51
问题 I'm trying to add rows to a matrix at particular locations, where the locations are contained in a vector. The schema below shows the inputs and the expected outcome. I tried to use a "for" loop but couldn't make it work. Any suggestions help. Source Matrix (6x3) (1) 1 2 3 (2) 4 5 6 (3) 7 8 9 (4) 6 9 2 (5) 3 6 1 (6) 2 2 7 Vector of locations (indicates the row in the source matrix that will contain zeros) [2, 5, 6] Result Matrix(6+length.vector x 3) (1) 1 2 3 (2*)0 0 0 (3) 4 5 6 (4) 7 8 9 (5*

Insert row at a specific location in matrix using R

烈酒焚心 提交于 2021-01-27 05:11:35
问题 I'm trying to add rows to a matrix at particular locations, where the locations are contained in a vector. The schema below shows the inputs and the expected outcome. I tried to use a "for" loop but couldn't make it work. Any suggestions help. Source Matrix (6x3) (1) 1 2 3 (2) 4 5 6 (3) 7 8 9 (4) 6 9 2 (5) 3 6 1 (6) 2 2 7 Vector of locations (indicates the row in the source matrix that will contain zeros) [2, 5, 6] Result Matrix(6+length.vector x 3) (1) 1 2 3 (2*)0 0 0 (3) 4 5 6 (4) 7 8 9 (5*