Multiplying by pattern matching

I have a matrix of the following format: matrix = np.array([1, 2, 3, np.nan], [1, np.nan, 3, 4], [np.nan, 2, 3, np.nan]) and coefficients I want to selectively multiply element-wise with my matrix: coefficient...
more »

2017-05-11 18:05 (1) Answers

Slow dot product in R

I am trying to take the dot product from a 331x23152 and 23152x23152 matrix. In Python and Octave this is a trivial operation, but in R this seems to be incredibly slow. N <- 331 M <- 23152 mat_1 = matrix( rnorm(N*M,mean=0,sd=1), N, M) mat_...
more »

2017-05-08 18:05 (3) Answers

summation of sub-matrices of boolean numpy arrays

I have a 11x51 boolean matrix a. On this I do this operation in Matlab to obtain a boolean matrix of size 10x50. a = logical(a(1:end-1,1:end-1) + a(2:end,1:end-1) + a(1:end-1,2:end) + a(2:end,2:end)) I want to do this in python. I tried this:- a ...
more »

2017-05-07 22:05 (2) Answers

Double loop to fill a correlation matrix

I have a dataset like this set.seed(1) a = abs(rnorm(10, mean = 0, sd= 1)) b = abs(rnorm(10, mean = 0, sd= 1)) c = abs(rnorm(10, mean = 0, sd= 1)) d = abs(rnorm(10, mean = 0, sd= 1)) df = as.data.frame(cbind(a, b, c, d)) And i want to get a table ...
more »

2017-05-03 20:05 (2) Answers

Sum nth columns elementwise in Numpy matrix

I have the following numpy matrix: A = [[a,b,c,d,e,f], [g,h,i,j,k,l], ...] I need to sum all the nth columns in a element-wise fashion. So, if n is 2, the answer needs to be: B = [[a+c+e, b+d+f], [g+i+k, h+j+l], ...] (like...
more »

2017-05-02 18:05 (1) Answers

Extract matrix column with it's name

Let's say you have a matrix defined as m1 = matrix( rbind(c(12,8,9),c(4100,3600,3200)), byrow=FALSE, nrow=2, ncol=3, dimnames=list(c("Days","Amount"),c("Col1","Col2","Col3")) ) Which yields: Col1 Col2 Col3 Days 12 8 ...
more »

2017-04-23 17:04 (2) Answers

Min Cost through a Matrix

Matrix: 6 9 3 4 8 2 3 5 10 You can start at any integer in the first row, you can only go down the matrix and add the left,right or directly below the previous number. For example, you start at 9 you can either go to 4,8, or 2. I figured out how to...
more »

2017-04-20 22:04 (1) Answers

Fast way of calculating distance between rows

I am trying to calculate weighted euclidean distance between rows from two dataframes. For example df.test contains 100 rows whiledf.train has 1000 rows.And I would like to find rows with mini distance in df.train for every row in df.test First: I...
more »

2017-04-19 10:04 (0) Answers

C - Split matrix into equipartitions

I'm writing an algorithm to divide a matrix into 4 submatrices(equipartitions). Size of the whole matrix is always multiplier of 4 as being shown in the added image. If you watch out, I'm trying to access elements like 1D element instead of 2D. I can...
more »

2017-04-16 10:04 (1) Answers

Interchange antisymmetric elements in a matrix

I am trying to change an element of an array from its position a (not sure if it said so) its antisymmetric position with respect to the center of the matrix, if the position to be exchanged forms part of the center nothing should be done (depending ...
more »

2017-04-15 00:04 (1) Answers