## Bin elements per row - Vectorized 2D Bincount for NumPy

I have a NumPy array with integer values. Values of matrix range from 0 to max element in matrix(in other words, all numbers from 0 to max data element presented in it). I need to build effective( effective means fast fully-vectorized solution) for s...
more »

## Python-check 3*3 matrix in a 9*9 matrix for duplicate, as in sudoku

My approach: use numpy.vsplit and hsplit to split the 9*9 into 9 3*3 blocks and use .flat function to flatten each 3*3 block, then use set() to check if the list contains duplicate import numpy as np def validSolution(board): b=np.array(boar...
more »

## Python: fastest way of adding columns containing pairwise products of column elements

Suppose I have a numpy array X = np.array([[1,2,3], [4,5,6], [7,8,9]]) I want to extend this matrix by adding (on the left) the columns resulting by multiplying together all possible pairs of columns. In this example it...
more »

## Python: Multiplying a list of vectors by a list of matrices as a single matrix operation

I have a list of 100 N-dimensional vectors and a list of 100 MxN matrices. So you can think of the two data structures as a 100xN list (or numpy array) and a 100xMxN list (or numpy array). What I want to do is take the dot product of each vector and...
more »

## Numpy multiply arrays into matrix (outer product)

I have 2 numpy arrays of shape (5,1) say: a=[1,2,3,4,5] b=[2,4,2,3,6] How can I make a matrix multiplying each i-th element with each j-th? Like: ..a = [1,2,3,4,5] b 2 2, 4, 6, 8,10 4 4, 8,12,16,20 2 2, 4, 6, 8,10 3 3, 6, 9,12,15 6 ...
more »

## Compute rotation matrices from arrays of "aim" and "up" vectors

I want to compute an array of rotation matrices from given arrays of aim and up vectors. For simplicity I'll assume the aim axes will correspond to the matrices's x components, and up axes to the matrices's y components. The only way that i know of...
more »

## Constructing a randomised matrix with no duplicates but fixed partial input

I´m facing a problem with constructing a randomised matrix where I partially already have values (that need to stay fixed - so no further randomisation there). Lets see: matrix should end up being 10 by 10 n <- 10 I do want my first rows...
more »

## Time difference between different subsetting methods for data.frame and matrix objects

Consider the following benchmark (R 3.4.1 on Windows machine): library(rbenchmark) mtx <- matrix(runif(1e8), ncol = 100) df <- as.data.frame(mtx) colnames(mtx) <- colnames(df) <- paste0("V", 1:100) benchmark( mtx[5000:7000, 80], m...
more »

## Numpy replace for loop by using a matrix to perform indexing

When dealing with a 3-dimensional matrix "M" of dimensions (A, B, C), one can index M using 2 vectors X with elements in [0, A) and Y with elements in [0, B) of the same dimension D. More specifically, I understand that when writing M[X,Y,:] we a...
more »

## VBA creation of a Matrix and double loop

I have a lot of Excel files in one folder and for each one I have (H:H) which contains a certain hour from 00:00 to 23:59 (appears within H at the format 0000 or 2359 etc ..) . Then, at a certain hour we associate a certain number of operation (conta...
more »

## Python: Edge List to an Adjacency Matrix using SciPy/Pandas shows IndexError: column index (3) out of bounds

I have a text file with an Edge List (egde.txt): 1 1 0.00000000000000000000 1 2 0.25790529076045041 1 3 0.77510411846367422 2 1 0.34610027855153203 2 2 0.00000000000000000000 2 3 0.43889275766016713 3 1 0.75335810231494713 3 2 0.22234924264075450 3 ...
more »

## Haskell - Sum of the differences between each element in each matrix

I am very new to Haskell (and functional programming in general) and I am trying to write a function called "profileDistance m1 m2" that takes two matrices as parameters and needs to calculate the sum of the differences between each element in each ...
more »

## Iterate over list of NumPy matrices

I've got a list with row matrices: rows = [ matrix([[1, 0, 0]]), matrix([[0, 1, 0]]), matrix([[0, 0, 1]]) ] and I attempted to loop over these using for (a, b, c) in rows:, but instead of this working, I got an error: ValueError: not enough valu...
more »

## caching the matrix value in R Permanently

I am working on caching a matrix and its inverse in R, I tried working on with the R.cache package but that doesn't give the result expected. I have included my code and the actual output that I am getting and also included the output that I expect p...
more »

## Replace matrix values for each array element

I have a 3-dimensional array arr where each array element is a matrix with the same number of rows and columns. Then I have a 3-column matrix m where the first two columns specify an index (row and column) and the last column a value. arr = array(0...
more »

## Recursive sum collums of matrix to single row (with zipWith (+))

lstsAdder :: [[Integer]] -> [Integer] lstsAdder [] = [] lstsAdder (x:xs) = zipWith (+) x (lstsAdder xs) As the title says, I want it to recursively add this: [[a,b,c],[d,e,f]] like this: [a+d,b+e,c+f], and this with lists of lists of any finite ...
more »

## Converting not a Square weighted adjacency matrix to igraph object in R

I am using tf_idf value to determine similarity between webpages.Till now I have my tf_idf matrix which is not square as there are many keywords but only 36 document .I want to convert this matrix to graph object so that i can take one mode projectio...
more »

## How to append group row into dataframe

I have this df1: A B C 1 2 3 5 7 9 where A B C are columns names. I have another df2 with one column: A 1 2 3 4 I would like to append df2 for each column of df1, creating this final dataframe: A B C 1 2 3 5 7 9 1 1 1 2 2 2 3 3 3 4 4 4 is ...
more »

## How do I merge a list and a csr matrix

I have a list of numbers and its len(lex) = 6064 that looks like this [0, 0, 1, 0, 0, -1, 1, 1, 0, 0, 0, 0, 1, 0,] and a csr matrix tweets.shape = (6064, 2500) How do I merge them I tried converting them to two lists both, but whe...
more »

## Equivalence between python for-loop and 3D numpy matrix additions

I cannot figure out a bug in a very simple transition from a for-loop to a vectorized numpy operation. The code is the following for null_pos in null_positions: np.add(singletree[null_pos, parent.x, :, :], posteriors[parent.u, nu...
more »

## Transform Pandas Dataframe to RCV

I have a dataframe that represents a covariance matrix s.th. like this names = ['AA','BB','CC','DD','EE'] x_ = np.random.normal(size=5) y_ = np.random.normal(size=5) z = np.vstack((x_, y_)) cov_mat = np.cov(z.T) cov_mat = np.triu(cov_mat, k=0) cov_...
more »

## What's the best way to create a "3D identity matrix" in Numpy?

I don't know if the title makes any sense. Normally an identity matrix is a 2D matrix like In [1]: import numpy as np In [2]: np.identity(2) Out[2]: array([[ 1., 0.], [ 0., 1.]]) and there's no 3rd dimension. Numpy can give me 3D matr...
more »

## Rearrange elements even and odd columns into one vector

I have a vector A8 with two columns in it, here are two rows: 24.0192018449307 48.0192018449307 72.0576055347920 96.0576055347920 I want to create a new vector A9 such that I get one column: 24.0192018449307 48.0192018449307 72.057605534792...
more »

## Extracting chunks from a matrix by columns

Say I have a matrix with 1000 columns. I want to create a new matrix with every other n columns from the original matrix, starting from column i. So let say that n=3 and i=5, then the columns I need from the old matrix are 5,6,7,11,12,13,17,18,19 an...
more »

## Fast subsetting of a matrix in R

I face the following problem: I need many subsets of a big matrix. Actually I just need views as input for another function f(), so I don't need to change the values. However it seems, that R is terribly slow for this task, or I'm doing something wr...
more »

## How to fill a matrix in Python using iteration over rows and columns

So I have an array of 5 integers v and another of 10 integers v. I have a 5 by 10 matrix P that I would want to fill so that (P)ij = v[i] + u[j] I tried: P = np.empty((len(asset_grid),len(asset_grid))) for i in range(asset_grid): for j in rang...
more »

## Is it possible to multiply all of each column in matrix A by each column of matrix B without for loop?

I have three matrices W H and V. I want to get keep which stores elementwise multiplication between all column of V and each column of W and do sum by row. (V has 6 rows, W also has 6 rows. Each column of W (which has 6 elements) multiplies with 6 ...
more »

## Can't save a numpy 2-d array into a file

I have the following 2-d numpy matrix, which was a concatenation of two matrices: >>> mnist1_train_final_data=np.hstack((y_train_mnist_ni,features_train_mnist1)) >>> type(mnist1_train_final_data) <type 'numpy.ndar...
more »

## Make numpy matrix more sparse

Suppose I have a numpy array np.array([ [3, 0, 5, 3, 0, 1], [0, 1, 2, 1, 5, 2], [4, 3, 5, 3, 1, 4], [2, 5, 2, 5, 3, 1], [0, 1, 2, 1, 5, 2], ]) Now, I want to randomly replace some elements with 0. So that I have an output like ...
more »

## r corrplot with clustering: default dissimilarity measure for correlation matrix

I used the R package corrplot to visualize the correlation matrix from my data. I involved the clustering of variables using the embedded option hclust. The invocation of the command was like this (plus various arrangements of titles, axes etc): co...
more »

## Lists and matrix using sapply

I have a perhaps basic questions and I have searched on the web. I have a problem reading files. Though, I managed to get to read my files, following @Konrad suggestions, which I appreciate: How to get R to read in files from multiple subdirectories ...
more »

## Python - Calendar View from Dates in Pandas dataframe

I need to get a 7 day calendar view from a pandas dataframe that contains a list of events. Below is just a sample of the dates. DatetimeIndex(['2017-05-15', '2017-05-12', '2017-05-07', '2017-05-15', '2017-05-17', '2017-05-17', '201...
more »

## Correlations for pairs of combinations

I have 22 variables, and I'd like to get the correlation scores, not as a matrix of correlation, but in a data frame, by pairs... I mean... Not like this v1 v2 v3 v4 v1 1 x x x v2 x 1 x x v3 x x 1 x v4 x x x 1 b...
more »

## How to allow (but not require) specific words in specific places using regex?

I have this regex validation expression (thanks to ClasG and Marvel308): See Link for examples. ^((?P\begin{(?\w+)}\s*(?:-?\d+\s*(?:&\s*|\\n))+\end{\g})|\$\s*(?P>matrix)\s*\$|\$\$\s*(?P>matrix)\s*\$\$|[\s*(?P>matrix)\s*]|(\s*(?P>matrix)\s*))\$ A...
more »