4

What I have: I have the list List123=[-13,3,12,1] and the 2-by-4-matrix Matrix123=numpy.zeros((2,4), dtype=Decimal).

What I want: I want to change all entries of the matrix to any entry of the list and then print it to the terminal. There are 4^(2*4)=65536 possible combinations. I want to print every combination.

How I do it now: Here is the my current code:

List123=[-13,3,12,1]
Matrix123=numpy.zeros((2,4), dtype=Decimal)
k=0
for k in List123:
 Matrix123[0,0]=k
 for k in List123:
 Matrix123[0,1]=k
 for k in List123:
 Matrix123[0,2]=k
 for k in List123:
 Matrix123[0,3]=k
 for k in List123:
 Matrix123[1,0]=k
 for k in List123:
 Matrix123[1,1]=k
 for k in List123:
 Matrix123[1,2]=k
 for k in List123:
 Matrix123[1,3]=k
 print Matrix123
 print " "

My question: What is a more compact way to write this in only a few lines of code? I need to do the same for a 23-by-27 Matrix. That would mean that I have to write code for 23*27=621 for-loops manually, if I don't find a more compact way.

asked Sep 12, 2017 at 14:17
2
  • how about a for loop which iteratoes from 0 to 3 for the last slice of Matrix, and another for loop for the first index, ranging from 0 to 1 ? Commented Sep 12, 2017 at 14:24
  • You do realize that 4^(27*23) ~= 6.x10^373 and printing that many items will finish sometime around the heat death of the universe? Commented Sep 12, 2017 at 15:11

3 Answers 3

3

I highly recommend you to use itertools in python build-in tools:

from itertools import product
import numpy as np
List123 = [-13, 3, 12, 1]
for i in product(List123, repeat=8):
 print(np.array(i).reshape(2, 4))

Here's a pure recursion function version:

import numpy as np
List123 = [-13, 3, 12, 1]
def dfs(depth=1, result=None):
 if result is None:
 result = []
 if depth == 9:
 print(np.array(result).reshape(2, 4))
 return
 for i in List123:
 dfs(depth + 1, result + [i])
dfs()

In both way, you have 65536 results.

answered Sep 12, 2017 at 14:26
Sign up to request clarification or add additional context in comments.

1 Comment

This is very elegant.
3

Using np.meshgrid:

def my_product(list, shape = (4,2)):
 r = np.product(shape)
 out = np.stack(np.meshgrid(*(List123,) * r))
 out = out.reshape(shape[::-1] + (-1,)).T
 for mat in list(out):
 print(mat)

Of course, if your shape is (23,27), this will cause a memerror even if len(list) = 2 as all those permutations would fill all the storage on earth a ridiculous number of times over, and printing them all would take until the heat death of the universe.

answered Sep 12, 2017 at 15:28

Comments

0

You can use itertools.product:

import itertools
list123 = [-13, 3, 12, 1]
for matrix in itertools.product(list123, repeat=8):
 print matrix

It will output all the possible solution of length 8 with -13, 3, 12 and 1. matrix will be a tuple with 8 numbers, being the possible solutions.

If you need it to output the result in the real numpy.matrix form, you can create them on the fly (even though it will take more time).

import numpy as np
for prod in itertools.product(list123, repeat=8):
 print np.matrix([prod[:4], prod[4:]])

Or using reshape to improve readability (Thanks to @MenglongLi)

for prod in itertools.product(list123, repeat=8):
 print np.reshape(prod, (2, 4))

It will gave you 65536 results as expected.

answered Sep 12, 2017 at 14:30

5 Comments

You really, really shouldn't use numpy.matrix anymore. Just use np.array
@juanpa.arrivillaga is it due to performance issues? Can you provide a link or something about the topic?
No, more of a design consideration. Anyway, that's what scipy recommends. Essentially, numpy.matrix only exists for people coming from matlab to have something similar to the corresponding data-structure there. However, numpy and the scipy stack don't really have heavy support for it, and often, functions will return numpy.ndarrays regardless of if you input a np.matrix
@juanpa.arrivillaga still, np.linalg.eigh return a np.matrix for the eigenvalue decomposition of an hermitian matrix, when it could return a 2D array. Why so?
@P.Camilleri I think a lot of the linalg functions do. Probably for historical reasons.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.