Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Error in Following python program

Reply
Thread Tools

Error in Following python program

 
 
Pramod
Guest
Posts: n/a
 
      09-04-2010
#/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[i][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[i][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
~
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-04-2010
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[i][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[i][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[i][j]

and no, they're not the same!
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-04-2010
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[i][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[i][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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why the following python program does not face any concurrencyproblems without synchronize mechanism? smith jack Python 1 07-10-2011 02:50 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
I am getting following error when I am building my C program friend.05 C Programming 6 09-30-2006 04:30 PM
python equivalent of the following program AndyL Python 6 05-12-2006 12:39 AM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57