By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,408 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Comparing a matrix (list[][]) ?

P: n/a
Hi,

How can I find the minus element greater than zero in a matrix, my
matrix is

matrix=
[9,8,12,15],
[0,11,15,18],
[0,0,10,13],
[0,0,0,5]

I dont want to use "min" function because each element in the matrix is
associated to (X,Y) position.

Thanks a lot.

jDSL

Jan 13 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
jairodsl wrote:
How can I find the minus element greater than zero in a matrix, my
matrix is

matrix=
[9,8,12,15],
[0,11,15,18],
[0,0,10,13],
[0,0,0,5]

I dont want to use "min" function because each element in the matrix is
associated to (X,Y) position.
What output are you expecting from your example matrix? If you are expecting
it to be 5 (the smallest positive element), using "min" is the way to do it:
>>matrix = [[9, 8, 12, 15],
... [0, 11, 15, 18],
... [0, 0, 10, 13],
... [0, 0, 0, 5]]
>>min(min(x for x in row if x 0) for row in matrix)
5

--
Roberto Bonvallet
Jan 14 '07 #2

P: n/a
Roberto Bonvallet:
What output are you expecting from your example matrix? If you are expecting
it to be 5 (the smallest positive element), using "min" is the way to do it:
>>matrix = [[9, 8, 12, 15],
... [0, 11, 15, 18],
... [0, 0, 10, 13],
... [0, 0, 0, 5]]
>>min(min(x for x in row if x 0) for row in matrix)
5
This looks a bit like homework...
To find the minimal number 0 of the matrix this seems better:

mat = [[9, 8, 12, 15],
[0, 11, 15, 18],
[0, 0, 10, 13],
[0, 0, 0, 5]]

print min(el for row in mat for el in row if el 0)

Note that this raises TypeError is the matrix doesn't have one or more
numbers 0
If the OP needs the position he/she can do something like:

nozeromin = 1e300 # or something similar
pos = None, None

for nrow, row in enumerate(mat):
for ncol, el in enumerate(row):
if el 0 and el < nozeromin:
nozeromin = el
pos = nrow, ncol

print nozeromin, pos

Bye,
bearophile

Jan 14 '07 #3

P: n/a
Ok, the main idea is compare each element with others elements, and go
saving the minus element positive and his position (X,Y) in the matrix,
of course min function dont let me save this position (X,Y). I dont
know if its possible do that with min function.

jDSL

Roberto Bonvallet wrote:
>
What output are you expecting from your example matrix? If you are expecting
it to be 5 (the smallest positive element), using "min" is the way to do it:
>>matrix = [[9, 8, 12, 15],
... [0, 11, 15, 18],
... [0, 0, 10, 13],
... [0, 0, 0, 5]]
>>min(min(x for x in row if x 0) for row in matrix)
5

--
Roberto Bonvallet
Jan 14 '07 #4

P: n/a
Thanks a lot, it was that i need !!! Works very good !

be************@lycos.com wrote:
Roberto Bonvallet:
What output are you expecting from your example matrix? If you are expecting
it to be 5 (the smallest positive element), using "min" is the way to do it:
>>matrix = [[9, 8, 12, 15],
... [0, 11, 15, 18],
... [0, 0, 10, 13],
... [0, 0, 0, 5]]
>>min(min(x for x in row if x 0) for row in matrix)
5

This looks a bit like homework...
To find the minimal number 0 of the matrix this seems better:

mat = [[9, 8, 12, 15],
[0, 11, 15, 18],
[0, 0, 10, 13],
[0, 0, 0, 5]]

print min(el for row in mat for el in row if el 0)

Note that this raises TypeError is the matrix doesn't have one or more
numbers 0
If the OP needs the position he/she can do something like:

nozeromin = 1e300 # or something similar
pos = None, None

for nrow, row in enumerate(mat):
for ncol, el in enumerate(row):
if el 0 and el < nozeromin:
nozeromin = el
pos = nrow, ncol

print nozeromin, pos

Bye,
bearophile
Jan 14 '07 #5

P: n/a
be************@lycos.com wrote:
mat = [[9, 8, 12, 15], ..., [0, 0, 0, 5]]

print min(el for row in mat for el in row if el 0)
...
If the OP needs the position he/she can do something like:
<< iterative solution >>

Or you can still use min by keeping the position after the value:

value, x_pos, y_pos = min( (elem, x, y)
for y, row in enumerate(mat)
for x, elem in enumerate(row)
if elem 0 )

You get the "first" entry with the minimal value.
This code raises ValueError if all entries of mat are <= 0.

--Scott David Daniels
sc***********@acm.org
Jan 14 '07 #6

P: n/a
jairodsl wrote:
Hi,

How can I find the minus element greater than zero in a matrix, my
matrix is

matrix=
[9,8,12,15],
[0,11,15,18],
[0,0,10,13],
[0,0,0,5]

I dont want to use "min" function because each element in the matrix is
associated to (X,Y) position.

Thanks a lot.

jDSL
You might consider numarray or numpy for this sort of thing.

Colin W.

Jan 14 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.