0

I'm trying to assign X name to N variables, through basic functions. I've been searching for the key but I always find the same answer:

Thats a bad idea, why dont u use a dictionary...

def create_matrix(m,nf,nc):
m = np.empty((nf,nc), dtype=np.int)
for i in range(nf):
    for j in range(nc):
        m[i][j] = int(input('Value: '))

i'd like to build N "matrices", i need to declare m = np.empty((nf,nc), dtype=np.int) before each one, the answer is, How could i "dinamize" that funcition in order to operate later with them? F.E:

create_matrix1(m1,2,3) create_matrix2(m2,2,3) . . . create_matrixn(mn,2,3) m1 + m2 + m5 = x

Thanks in advance

Well, that works, thank you!

>>> mats
[array([[1, 2],
       [3, 4]]), array([[5, 6],
       [7, 8]]), array([[ 9, 10],
       [11, 12]])]

Now I'm trying to figure how to refer mats[1(x,y)] in order to operate with them

>>> mats[1[[1][1]]]
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    mats[1[[1][1]]]
IndexError: list index out of range
>>> mats[1[1][1]]
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    mats[1[1][1]]
TypeError: 'int' object is not subscriptable

You are right, again.

>>> mats[1][1][1]
8

I thought i tried every posibility, i think im tired, time to sleep.

Thank you very much!

2
  • You don't have to give a name to the matrices, as long as you can keep a reference to them that is sufficient. Store the references in e.g. a list, or a dictionary. Commented Jan 10, 2016 at 23:32
  • where you would have used m1(x,y) uses mats[1](x,y) - the mats[1] replaces the m1, if you see what I mean. Or where you would have used m1[x][y], use mats[1][x][y] Commented Jan 11, 2016 at 0:11

1 Answer 1

4

Because you don't know how many of the matrices there are, giving them names isn't going to work, is it? Also it gets unwieldy very quickly, if say there were 43 matrices you'd have to use variable names such as m1 to m43 and type those names in when needed, and do a whole load more typing if 44 are needed next time, or what happens if you only need 3 another time? In fact it's a feature of Python (and other programming languages) that it is possible to store a reference to something like a matrix (also objects, functions, ...), so you don't have to give a name to the matrices, as long as you can keep a reference to them that is sufficient. Store the references in e.g. a list, or a dictionary.

Then, instead of passing in the matrix m, make the function return the reference to the new matrix, and calling code stores the references in perhaps a list, like this:

def create_matrix(nf,nc):
    m = np.empty((nf,nc), dtype=np.int)
    for i in range(nf):
        for j in range(nc):
            m[i][j] = int(input('Value: '))
    return m

mats = []
# for this example let's create 43 matrices
for i in range(43):
    mats.append(create_matrix(100,13)
print mats

Now mats[0] is the first of these, mats[1] the second, and so on.

One way to think of this is that where you want to create a dynamic matrix m1, that is actually stored in mats[0] in my example, m2 is in mats[1], m3 in mats[2]. (because lists are indexed starting at 0, the first item is in mats[0])

You could also use this same function to create matrices in the style you wanted, e.g.:

m1 = create_matrix(100,13)
m2 = create_matrix(100,13)
m3 = create_matrix(100,13)
...

To refer to these matrices, where you would have used e.g. m1(x,y) use mats[1](x,y) - the mats[1] replaces the m1, if you see what I mean. Or where you would have used m1[x][y], use mats[1][x][y]

Sign up to request clarification or add additional context in comments.

2 Comments

It's more natural to use list comprehension instead of using append on mats. mats = [create_matrix(100,13) for x in range(43)]
Yes I agree that's more natural once you're used to Python, but can be confusing for someone new to it - I was trying to keep it simple.

Your Answer

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.