Error in Following python program

Discussion in 'Python' started by Pramod, Sep 4, 2010.

  1. Pramod

    Pramod Guest

    #/usr/bin/python
    from numpy import matrix
    n=input('Enter matrix range')
    fr=open('mat.txt','r')
    print ('Enter elements into the matrix\n')
    a=matrix([[input()for j in range(n)] for i in range(n)])
    for i in range(n):
    for j in range(n):
    print a[j]
    print '\n'

    When i run the above program the following error is Coming please
    Error is
    Enter matrix range3
    Enter elements into the matrix

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [[1 2 3]]
    Traceback (most recent call last):
    File "2.py", line 10, in <module>
    print a[j]
    File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
    line 265, in __getitem__
    out = N.ndarray.__getitem__

    please resolve my problem Thanks in advance
    ~
     
    Pramod, Sep 4, 2010
    #1
    1. Advertising

  2. Pramod

    MRAB Guest

    On 04/09/2010 19:28, Pramod wrote:
    > #/usr/bin/python
    > from numpy import matrix
    > n=input('Enter matrix range')
    > fr=open('mat.txt','r')
    > print ('Enter elements into the matrix\n')
    > a=matrix([[input()for j in range(n)] for i in range(n)])
    > for i in range(n):
    > for j in range(n):
    > print a[j]
    > print '\n'
    >
    > When i run the above program the following error is Coming please
    > Error is
    > Enter matrix range3
    > Enter elements into the matrix
    >
    > 1
    > 2
    > 3
    > 4
    > 5
    > 6
    > 7
    > 8
    > 9
    > [[1 2 3]]
    > Traceback (most recent call last):
    > File "2.py", line 10, in<module>
    > print a[j]
    > File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
    > line 265, in __getitem__
    > out = N.ndarray.__getitem__
    >
    > please resolve my problem Thanks in advance
    > ~

    The matrix is 2-dimensional, which in numpy is means you need to write:

    a[i, i]

    not:

    a[j]

    and no, they're not the same! :)
     
    MRAB, Sep 4, 2010
    #2
    1. Advertising

  3. Pramod

    Peter Otten Guest

    Pramod wrote:

    > #/usr/bin/python
    > from numpy import matrix
    > n=input('Enter matrix range')
    > fr=open('mat.txt','r')
    > print ('Enter elements into the matrix\n')
    > a=matrix([[input()for j in range(n)] for i in range(n)])
    > for i in range(n):
    > for j in range(n):
    > print a[j]
    > print '\n'
    >
    > When i run the above program the following error is Coming please
    > Error is
    > Enter matrix range3
    > Enter elements into the matrix
    >
    > 1
    > 2
    > 3
    > 4
    > 5
    > 6
    > 7
    > 8
    > 9
    > [[1 2 3]]
    > Traceback (most recent call last):
    > File "2.py", line 10, in <module>
    > print a[j]
    > File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
    > line 265, in __getitem__
    > out = N.ndarray.__getitem__
    >
    > please resolve my problem Thanks in advance


    You can either use an array instead of a matrix and continue to access the
    elements like you did in your code

    >>> a = numpy.array([[1,2],[3,4]])
    >>> a[1][1]

    4

    or continue to use the matrix and access its elements with a tuple

    >>> b = numpy.matrix([[1,2],[3,4]])
    >>> b[1,1]

    4

    If you pass only one index you get another, smaller matrix:

    >>> b[1]

    matrix([[3, 4]])

    Once you see this printed it should be clear that b[1][1] asks for the non-
    existent second row of the above matrix. Hence the error:

    >>> b[1][1]

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py", line 265,
    in __getitem__
    out = N.ndarray.__getitem__(self, index)
    IndexError: index out of bounds

    By the way, these matrices are really strange beasts:

    >>> b[0][0]

    matrix([[1, 2]])
    >>> b[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]

    matrix([[1, 2]])

    Peter
     
    Peter Otten, Sep 4, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bonds
    Replies:
    2
    Views:
    2,795
    Amaryllis
    Aug 3, 2004
  2. AndyL
    Replies:
    6
    Views:
    347
    Edward Elliott
    May 12, 2006
  3. friend.05
    Replies:
    6
    Views:
    297
    Richard Heathfield
    Sep 30, 2006
  4. smith jack
    Replies:
    1
    Views:
    194
    TheSaint
    Jul 10, 2011
  5. HP
    Replies:
    0
    Views:
    304
Loading...

Share This Page