2 Replies - 1922 Views - Last Post: 19 September 2012 - 12:55 PM Rate Topic: -----

#1 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Matrix Dimension Error

Posted 18 September 2012 - 04:32 PM

let's see... I'm making a matrix generating function where it takes a null array "[]" and builds it up to an array within an array with the appropriate dimensions to model a NxM matrix. For the time being, it will take the array/matrix/whatever and fill it with zeroes, so if say its a 4x6 matrix, I should see 4 rows of arrays filled with 6 zeroes contained, or if I do visa versa with a 6x4 matrix, I should see 6 rows of arrays with 4 zeroes contained. Now the problem I seem to be encountering with my method is it will only allow a matrix (NxM) to be generated with the value of "N" being always greater than "M". If I try to do the reverse, it will build the array only up to a point of the "N" value and halt with an IndexError. Any tips on circumventing this?

Error Message:
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    createMatrix(4,9)
  File "C:\Python\workbench\NxN_det.py", line 12, in createMatrix
    null_matrix[j].append(0)
IndexError: list index out of range



And here's a sample of what I've been working with:
def createMatrix(row,column):
    null_matrix = []
    add = [0]
    sub = []
    k = 0

    for i in range(row):
        null_matrix += add
        null_matrix[i] = sub

    for j in range(column):
        null_matrix[j].append(0)
        print null_matrix


Although this code works perfectly for what I intend to do for my determinant solving method of NxN matrices (being as it does generate nicely for all N >= M), being as it may, I prefer to make this dynamic enough for any matrix function i may need to utilize later... :\

This post has been edited by BloodyInari: 18 September 2012 - 04:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Matrix Dimension Error

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: Matrix Dimension Error

Posted 19 September 2012 - 06:40 AM

Trace it:
>>> def createMatrix(row,column):
...     print "createMatrix:", row, column
...     null_matrix = []
...     for i in range(row):
...             print "row=", i
...             null_matrix += [0]
...             print null_matrix
...             null_matrix[i] = []
...             print null_matrix
...     for j in range(column):
...             print "col=", j
...             null_matrix[j].append(0)
...             print null_matrix
...     return null_matrix
... 
>>> createMatrix(4,9)
createMatrix: 4 9
row= 0
[0]
[[]]
row= 1
[[], 0]
[[], []]
row= 2
[[], [], 0]
[[], [], []]
row= 3
[[], [], [], 0]
[[], [], [], []]
col= 0
[[0], [], [], []]
col= 1
[[0], [0], [], []]
col= 2
[[0], [0], [0], []]
col= 3
[[0], [0], [0], [0]]
col= 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in createMatrix
IndexError: list index out of range
>>> 



Clearly the + [0] or the = [] is pointless.

However, I think you have an indent issue. So:
>>> def createMatrix(row,column):
...     print "createMatrix:", row, column
...     null_matrix = []
...     for i in range(row):
...             print "row=", i
...             null_matrix += [0]
...             print null_matrix
...             null_matrix[i] = []
...             print null_matrix
...             for j in range(column):
...                     print "col=", j
...                     null_matrix[j].append(0)
...                     print null_matrix
...     return null_matrix
... 
>>> 
>>> createMatrix(4,9)
createMatrix: 4 9
row= 0
[0]
[[]]
col= 0
[[0]]
col= 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in createMatrix
IndexError: list index out of range



Hmm... that was dramatically bad. Perhaps you want i instead of j?
>>> createMatrix(4,9)
createMatrix: 4 9
row= 0
[0]
[[]]
col= 0
[[0]]
col= 1
[[0, 0]]
col= 2
[[0, 0, 0]]
col= 3
[[0, 0, 0, 0]]
col= 4
[[0, 0, 0, 0, 0]]
col= 5
[[0, 0, 0, 0, 0, 0]]
col= 6
[[0, 0, 0, 0, 0, 0, 0]]
col= 7
[[0, 0, 0, 0, 0, 0, 0, 0]]
col= 8
[[0, 0, 0, 0, 0, 0, 0, 0, 0]]
row= 1
[[0, 0, 0, 0, 0, 0, 0, 0, 0], 0]
[[0, 0, 0, 0, 0, 0, 0, 0, 0], []]
col= 0
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0]]
col= 1
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0]]
col= 2
[[0, 0, 0, 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 0]]
col= 7
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
col= 8
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> 



That looks like the stuff!

Now, cleaned up:
>>> def createMatrix(row,column):
...     null_matrix = []
...     for i in range(row):
...             null_matrix.append([]) # unambiguous, does what we want
...             for j in range(column):
...                     null_matrix[i].append(0)
...     return null_matrix
... 
>>> 
>>> createMatrix(4,9)
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> 




Note, there is a one liner that can do this for you:
>>> def createMatrix(row,column):
...     return [ [ 0 for c in range(column) ] for r in range(row) ]
... 
>>> 
>>> createMatrix(4,9)
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> 



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#3 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Matrix Dimension Error

Posted 19 September 2012 - 12:55 PM

Quote

Hope this helps.


Above and beyond the scope of what I needed. Much thanks. :lol:

This post has been edited by BloodyInari: 19 September 2012 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1