## creating a square matrix from a data frame

I'm having trouble to make from my data.frame a square matrix. Now my data looks something like this: var1 var2 value A B 4 C D 5 D A 2 B D 1 I'm trying to transform the data.frame to a matrix that l...
more »

## Numpy: Insert value into different column for each row

I'm trying to insert into a numpy matrix given a mask that defines a single cell per row. Effectively, it's inserting a value into each row but with a different column. I've tried to use np.insert() without success: >>> x array([[False, Fal...
more »

## What's the most efficient way to identify a “default” position on a board that can be flipped and rotated?

I am currently implementing an AI playing a simple board game (5 in a row) in Python 3.6. The board is 7x7 and each field can contain either a black or a white stone. To simplify the problem, I want to treat rotated or flipped board positions the sa...
more »

## How to add columns to a big numpy matrix

I happen to have a 2-D numpy matrix of the form 2 3 1 2 3 9 . . I need to add another column to it so that it represents the squared sum of the first two columns like : 2 3 13 1 2 5 3 9 36 . . what numpy functions should I use to add this th...
more »

## What is the best way to create a block matrix form a row vector?

I have the following numpy row matrix. X = np.array([1,2,3]) I want to create a block matrix as follows: 1 0 0 2 1 0 3 2 1 0 3 2 0 0 3 How can I do this using numpy? ...
more »

## How can you quickly map the indices of a banded matrix to a 1-dimensional array?

This is closely related to a the question: How to map the indexes of a matrix to a 1-dimensional array (C++)? I need to assign a reversible index to each non-zero element in a banded matrix. In the normal, full matrix it is easy to do: ...
more »

## Segmentation fault using scanf for float ** type

I'm trying to code a function that would ask the user to enter matrix. It prompts the number of lines, the number of columns and then prompts the value for each element of the matrix: #include <stdio.h> #include <stdlib.h> void enterMat...
more »

## ScikitLearn regression: Design matrix X too big for regression. What do I do?

I have a matrix X that has something like 7000 columns and 38000 rows. Thus it is a numpy array with shape (38000, 7000). I instantiated the model model = RidgeCV(alphas = (0.001,0.01, 0.1, 1) and then fitted it model.fit(X, y) where y is the ...
more »

## Mean of data.table column values as specified using a matrix

I have a data.table containing the x,y,z values of 10,000 points (for this example) in the unit cube, and each point has a corresponding attribute (called P). I've used nn2 from the RANN package to find the k-neighbors (up to 50) indices of each poin...
more »

## How to store multidimensional subscript as variable in R

Suppose I have a matrix, mat <- matrix((1:9)^2, 3, 3) I can slice the matrix like so > mat[2:3, 2] [1] 25 36 How does one store the subscript as a variable? That is, what should my_sub be, such that > mat[my_sub] [1] 25 36 A list g...
more »

## solving Ax =b for a non-square matrix A using python

I'm focusing on the special case where A is a n x d matrix (where k < d) representing an orthogonal basis for a subspace of R^d and b is known to be inside the subspace. I thought of using the tools provided with numpy, however they only work with...
more »

## Plot eigenvalues of matrix as function of matrix element

I have never used python before, but need it now because I'm working on a colleague's project. I want to plot the eigenvalues of a matrix as a function of an element of the matrix, i.e. my matrix looks like M=[[40,0,4],[0,0,4],[4,4,x]] where x...
more »

## Using numpy.einsum for transpose times matrix: x^T * x

For a 2D matrix X (shape (m,n)), I'm trying to calculate X.T * X where * is matrix multiplication. Following the explanation on this post I expected to be able to do this using, np.einsum('ji,ik->jk', X, X) where on the LHS writing ji first takes...
more »

## Diagonalization of a tridiagonal, symmetric sparse matrix with Python

I have an NxN symmetric and tridiagonal matrix computed by a Python code and I want to diagonalize it. In the specific case I'm dealing with N = 6000, but the matrix can become larger. Since it is sparse, I assumed the best way to diagonalize it wa...
more »

## Achieving batch matrix multiply using tensordot

I'm trying to achieve the same behaviour as np.matmul parallel matrix multiplication using just tensordot,dot and reshaping etc. The library I am translating this to using does not have a matmul that supports parallel multiplication, only dot and te...
more »

## Construct matrix using selection of rows and columns in numpy

In NumPy, suppose I have a matrix X: X = array([[3, 1, 4, 5], [5, 1, 2, 1], [4, 4, 0, 1], [0, 3, 0, 3], [1, 2, 3, 4]) How can I construct a new matrix using the first (row 0), last second and last (row 3, 4) of X. The resulting matrix is Y = array(...
more »

## Numpy: Alternative ways to construct a matrix of points given the matrix of each coordinate separately

Let x, y, z be matrix representations, so that (x[i, j], y[i, j], z[i, j]) corresponds to a certain point. Instead of having 3 variables we want to have just one variable (Points) where "Points[i,j]=(x[i,j],y[i,j],z[i,j])" and "Points[i,j,0]=x[i,j]...
more »

## Storing a variable amount of matrices in python using numpy

I am trying to recreate some code from MatLab using numpy, and I cannot find out how to store a variable amount of matrices. In MatLab I used the following code: for i = 1:rows K{i} = zeros(5,4); %create 5x4 matrix K{i}(1,1)= ET(i,1); %pu...
more »

## 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 »

## Computing the null space of a bigmatrix in R

I can not find any function or package to calculate the null space or (QR decomposition) of a bigmatrix (from library(bigmemory)) in R. For example: library(bigmemory) a <- big.matrix(1000000, 1000, type='double', init=0) I tried the following...
more »

## return matrix on C

Here is my code below. #include <stdio.h> #define _USE_MATH_DEFINES #include <math.h> void rot(int angle); static double R[3][3]={0}; int main(void) { int angle = 30; rot(angle); int i, j = 0; for (i = 0; i < 3;i++) ...
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 »