## Find all first, then all second, then all third etc. elements of lists in a list

Working with a self-made definition of a (9x9) matrix described as a list of lists of Maybe Ints. I want to create a function that returns the 9 columns of the matrix. I want to do this something like: cols :: Matrix a -> [Block a] cols matrix = ...
more »

## Creating an occurrence matrix for each char from a large number of equal length strings

I am trying to create a matrix which gives me the occurrence of each element at each position, based on a large number of strings in a vector. I have the following pet example and potential solution: set.seed(42) seqs <- sapply(1:10, FUN = func...
more »

## Fast way to set diagonals of an (M x N x N) matrix? Einsum / n-dimensional fill_diagonal?

I'm trying to write fast, optimized code based on matrices, and have recently discovered einsum as a tool for achieving significant speed-up. Is it possible to use this to set the diagonals of a multidimensional array efficiently, or can it only re...
more »

## Numpy "vectorized" row-wise dot product running slower than a for loop

Given a matrix A with shape (n,k) and a vector s of size n, I want to compute a matrix G with shape (k,k) as follows: G += s[i] * A[i].T * A[i], for all i in {0,...,n-1} I tried to implement that using a for loop (Method 1) and in a vectorized mann...
more »

## Finding common links in a matrix and classification by common intersection

Suppose I have a matrix of distance cost, in which the cost of destiny and the cost of origin both need to be below a certain threshold amount - say, US 100 -- to share a link. My difficulty lies in achieving a common set after classifying these loca...
more »

## How get matrix of p-values for multiple Stuart-Maxwell test in R? Ask

I have a data set including 1o questions (Q1-Q10). I would like to extract P-values of pairwise Stuart-Maxwell tests and print them in a matrix with headers of column names in a way I can detect which P-value refers to which pairwise comparison. I ha...
more »

## Python Pandas Building full matrix of contrasts

I have a specific query using Python Pandas. Apologies for the poor presentation I have a df like that CG | T ------ 10 | 0.5 21 | 0.2 33 | 0.3 45 | 0.6 I would like to build all the contrasts possibles for CG...
more »

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

## I accidentaly change a variable with a conditional sentence ( if )?

Information about the code down below: the function initialise players code is: void initialise_players (tvector_players *vj){ int i; for (i=0; i< (vj->nplayers); i++){ vj->vjug [i].letter = 'A'+i; vj->vjug [i].num_region...
more »

## Division with numpy matrices that might result in nan

How can I divide two numpy matrices A and B in python when sometimes the two matrices will have 0 on the same cell? Basically A[i,j]>=B[i,j] for all i, j. I need to calculate C=A/B. But sometimes A[i,j]==B[i,j]==0. And when this happens I need A[...
more »

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

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

## How to transpose a matrix without using t() function in r

I know theres a function on R that allows you to transpose any matrix but was wondering how I would go about transposing a matrix without using this (ie. making own function) How would I write this as my own function? transpose <- function(M) ...
more »

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

## How to create a method for making a distance matrix in Python?

I am trying to make a distance matrix method within a class in Python. However, I don't know how to. It has to take a set of class objects as an argument and create a distance matrix out of them. This is for biological applications involving FASTA fi...
more »

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

## How do you write a matrix using a "for" loop in R?

So for data evaluation that I am doing at the moment I want to write a matrix using a "for" loop. Let's say I have random numbers between 0 and 100: E <- runif(100, 0, 100) t <- 0 #start for(t in 0:90) { D <- length(E[E >= t, E &l...
more »

## Panda: add a sublevel to an index that depend from the upper one

Possible duplicates but the solution provided there cannot fit my problem due to the information I get. The idea is quite simple. I have a matrix with a multilevel (and in my case I didn't build the index, I get only the DataFrame): #test = (('2','...
more »

## How to make sum with variable finite number of elements?

This is one of the first things I try to code in python (and any programming language) and my first question here, so I hope I provide everything neccessary to help me. I have upper triangular matrix and I need to solve system of equations Wx=y, whe...
more »

## How to create Bounded instance based on runtime values?

I've been playing with O'Connor's matrix implementation based on *-semirings, allowing very neat solutions for graph algorithms: import Data.Array newtype Matrix i e = Matrix (Array (i,i) e) matrix :: (Ix i, Bounded i) => ((i,i) -> e) -> M...
more »

## R: Apply function to matrix with elements of vector as argument

Suppose I want to apply a function to each row of a matrix. One of the function's arguments takes a vector. I would like to apply the first element of the vector to the first row, the second element to the second row, etc. For example: set.seed(12...
more »

## Matrix become (3,) instead of (6,3) in python Numpy

BPC=[BRad.*cos(BC)' BRad.*sin(BC)' zeros(1,6)'] here is my matlab code i can convert into python BPC=np.transpose(np.array([BRad * np.cos(BC),BRad * np.sin(BC),np.zeros((1,6))])) mathlab code gives (6,3) matrix and python gives (3,) matrix what...
more »

## Is there any way to store a matrix column-major in C or any alternative method?

In C, a matrix is stored in row-major order. It's a standard usage. Some languages such as FORTRAN store matrices in column-major order. Sometimes, it helps to access matrices in column order, in my programs. So is there any way to force C to store a...
more »

## How do I get the whole matrix in VBA from a pointer returned by a C++ DLL?

I have written a piece of code in C++ for a DLL that gets from VBA an array as input, and creates a new one, which in turn is given back to VBA. The DLL .h is written as follows: // FieldTrans.h #ifdef FIELDTRANS_EXPORTS #define FIELDTRANS_API __d...
more »

## How to identify columns that are sums of other columns in a dataset

I would like to write a function (preferably in R, but other languages are welcome), which would identify relationships between columns (limited to additions/substractions) in a dataset. A practical application of this would be to run it on large mul...
more »

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

## Checking every field in a "matrix" (with restriction)

i just learned haskell yesterday and i'm stuck with a task. i have a Matrix given; implemented with a function like this: board 1 1 = 0 board 1 2 = 1 board 1 3 = 0 board 2 1 = 2 board 2 2 = 0 board 2 3 = 0 board 3 1 = 1 board 3 2 = 0 board 3 3 = 2 b...
more »

## Why does matrix multiplication give different results depending on how they are grouped?

We know that A*B*C = A*(B*C), but why this matrix multiplication got different result? import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[1,2,3],[4,5,6],[7,8,9]]) print( A.dot( np.linalg.inv(B) ).dot(A.T) ) print( A.dot( np.linalg...
more »

## Sum of absolute off-diagonal differences in numpy matrix

I have a 2d numpy matrix and want to calculate the following test statistic. I have brute-force code to do it, but it seems like there should be a more general numpy solution that works for any 2D matrix, using things like np.diag(). I can't figur...
more »

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

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

## C++ - Function to multiply different sets of matrices with different dimensions

I have a few matrices defined as: array <array <float, 3>, 23> mat1; array <array <float, 23>, 3> mat2; array <array <float, 2>, 23> mat3; array <array <float, 23>, 2> mat4; I have a matrix multiplic...
more »

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

## Sequence of division and multiplication in matrices and vectors

I have (in python) the following operation (using numpy for matrices and vectors): result = (A.dot(input)/b) with A a matrix and b, input a vector. A and b are fixed, input is changing. Thus I would like to reduce A and b to a single element, eith...
more »