Comparing a matrix (list[][]) ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jairodsl

    #1

    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

  • Roberto Bonvallet

    #2
    Re: Comparing a matrix (list[][]) ?

    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

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Comparing a matrix (list[][]) ?

      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

      Comment

      • jairodsl

        #4
        Re: Comparing a matrix (list[][]) ?

        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

        Comment

        • jairodsl

          #5
          Re: Comparing a matrix (list[][]) ?

          Thanks a lot, it was that i need !!! Works very good !

          bearophileHUGS@ 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

          Comment

          • Scott David Daniels

            #6
            Re: Comparing a matrix (list[][]) ?

            bearophileHUGS@ 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
            scott.daniels@a cm.org

            Comment

            • Colin J. Williams

              #7
              Re: Comparing a matrix (list[][]) ?

              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.

              Comment

              Working...