473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing a matrix (list[][]) ?

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
6 1411
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ivan Voras | last post by:
Is there a nice(r) way of creating a list of uniform values? I'm currently using: list('0'*N), which makes a string and then chops it up into a list. I need it to create a NxN matrix: matrix = ...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
1
by: Nishant | last post by:
Hi there, I am looking forward to your help for comparing matrix in C++ I want to find common elements between 2 or more different matrices,using C++ for eg: A=
1
by: blazted | last post by:
I am trying to find our how I would compare two matrix's. I have two matrix and I want to find out if they are equal, less or greater than. I created a double loop to loop through and compare...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
14
by: Thomas Bauer | last post by:
Hello, I search a example like that. (1,0,0,1,4,5 ) -- moving (1,0,0,1,0,0 ) -- normal matrix (-1,0,0,1,0,0 ) -- Mirror Y-Axis (0,-1,0,1,0,0 ) -- Mirror X-Axis I caluculate all in mm.
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.