How to store numpy objects ( matrices ) fast?

I have to store a list of numpy matrices. Two approaches I tried: 1.Create a list and append to it: ls_ws=[] for ind in range(iterations): ... ls_ws.append(ls_w) # ls_w is a matrix of size 6,1 Problem: Fast at start, gets very slow at end...
more »

2017-03-25 13:03 (2) Answers

4d Array Processing (using einsum?)

I have a matrix-based problem which I think could be solved (computationally cheaply) in a single line of code using numpy (perhaps einsum?), but can't get to the solution. I wonder if anyone can make any suggestions please? The problem is as follo...
more »

2017-03-24 13:03 (0) Answers

error and output issues for python matrix?

I am building a function that takes a list made up of lists (ex: [['a'],['b'],['c']]) and outputs it as a table. I cannot use pretty table because I need a specific output (ex | a | b | ) with the lines and the spaces exactly alike. Here is my func...
more »

2017-03-18 21:03 (2) Answers

Swap zeros in numpy matrix

I have a numpy matrix like so: array([[2, 1, 23, 32], [34, 3, 3, 0], [3, 33, 0, 0], [32, 0, 0, 0]], dtype=int32) Now I want to shift all the numbers to the right and swap the zeros to the left like so: array([[2, 1, 23, 32]...
more »

2017-03-17 14:03 (6) Answers

Output Error for Matrix in Python?

I am trying to create a function that outputs a matrix that contains each item in a list on a separate line with lines in between. The only output I'm getting is quotations (''). I do not understand why. I think I set it all up correctly to output wh...
more »

2017-03-16 01:03 (1) Answers

Make a matrix of different list entries in r

I have a list of different matrices like this one: m1 = matrix(1:4, ncol = 2, byrow = T) m2 = matrix(11:14, ncol = 2, byrow = T) m3 = matrix(101:104, ncol = 2, byrow = T) m4 = matrix(1001:1004, ncol = 2, byrow = T) L = list(m1,m2,m3,m4) I wou...
more »

2017-03-15 16:03 (4) Answers

Iterate in submatrices through a bigger matrix

I looking for a way in which I using a for loop can iterate through a bigger matrix, in which each iteration will output a sub matrix of size (row, col, depth) (6,3,3). My big matrix is stored as numpy matrix, and would possible also like the each ...
more »

2017-03-15 15:03 (1) Answers

interweave 3 numpy matrices?

How do i interweave numpy matrices columwise. given this example: >>> import numpy as np >>> a = np.zeros((3,3)) >>> b = np.ones((3,3)) >>> c = b*2 should the interweaved output be [[ a[0,0]. b[0,0]. c[0,0]...
more »

2017-03-14 16:03 (2) Answers

If...else within a for loop

I am writing a function to perform bit inversion for each row of a binary matrix which depends on a predefined n value. The n value will determine the number of 1 bits for each row of the matrix. set.seed(123) ## generate a random 5 by 10 binary mat...
more »

2017-03-12 11:03 (1) Answers

Why sampling matrix row is very slow?

I tried to do some bootstrapping and calculate colMeans, naturally I chose matrix to store data, however, it is very slow in sampling: m[sample(n,replace=TRUE),] It turns out data.table is the fastest. require(microbenchmark) require(data.table) ...
more »

2017-03-06 06:03 (0) Answers

Can I use xor on numpy matrices?

I have 2 numpy matrix like this. matrix1 arr1 = array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 1., 0.]]) matrix2 arr2 = array([[ 0., 0., 0.], [ 0., 0., 1.], [ 0., 0., 0.]]) I want to find similarity of these...
more »

2017-03-01 19:03 (1) Answers