Home > Blockchain >  Why are my matrix values being reset back to 1?
Why are my matrix values being reset back to 1?

Time:04-29

I can't seem to figure out why the values of the first column after the 3 are being reset to 1 and then 1 again. I think it has to do with the not 999 if statement, but I'm not sure what to add or change. Any help is appreciated thank you!

n = 15 #the number the matrices will be an n*n of
a = [[ 0 for x in range(n)] for x in range(n)]
a[-1][-1] =111
a[0][3] =999

def Manhattan(x):
    k = 1
    m = 1
    for i in range(n):
        for j in range(n):
            if (x[i][j] < 1):
                if (x[j][i] < 1):
                    if (x[i][j-1] != 999 or  x[j-1][i] !=999 ):
                            k = x[i][j-1]
                            x[i][j] = k   1
                            m = x[j-1][i]
                            x[j][i] = m   1
                else:
                    k = x[i][j]
                    x[i][j] = k   1
    for row in x:
        print()
        for val in row:
            print('M' %val, end = " ")

Manhattan(a)

CodePudding user response:

im not completely sure what your program is meant to do but i think this is what youre trying to achieve as output:

   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 
   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16
   3    4    5    6    7    8    9   10   11   12   13   14   15   16   17
   4    5    6    7    8    9   10   11   12   13   14   15   16   17   18
   5    6    7    8    9   10   11   12   13   14   15   16   17   18   19
   6    7    8    9   10   11   12   13   14   15   16   17   18   19   20
   7    8    9   10   11   12   13   14   15   16   17   18   19   20   21
   8    9   10   11   12   13   14   15   16   17   18   19   20   21   22
   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23 
  10   11   12   13   14   15   16   17   18   19   20   21   22   23   24
  11   12   13   14   15   16   17   18   19   20   21   22   23   24   25
  12   13   14   15   16   17   18   19   20   21   22   23   24   25   26
  13   14   15   16   17   18   19   20   21   22   23   24   25   26   27
  14   15   16   17   18   19   20   21   22   23   24   25   26   27   28
  15   16   17   18   19   20   21   22   23   24   25   26   27   28   29 

code:

n = 15 #the number the matrices will be an n*n of
a = [[ 0 for x in range(n)] for x in range(n)]
a[-1][-1] = 111
a[0][3] = 999

def Manhattan(x1):
    start_value = 0

    for x in range(n):
        start_value  = 1
        second_val = start_value
        for y in range(n):
            x1[y][x] = second_val
            second_val  = 1

    for line in x1:
        for char in line:
            print('M' %char, end = " ")
        print()

Manhattan(a)

your code is fairly unorganized it makes it hard to understand but if this was your goal then what you need to do is increment a value starting from 1 by 1 each outer loop while looping through the matrix and then every iteration of the inner loop you increase it by 1 and set the value into the corresponding index.

CodePudding user response:

I think your question is why x[0][3] equals 1 after calling the function Manhattan, well that because your else statement is compare with if (x[j][i] < 1) statement.

So, when the code go on x[3][0], the if (x[0][3]) will equal False, then go to the else statement, that means: k=x[3][0]=0, and then x[3][0]=0 1=1.

I'm not sure if this output is your expectation:

   1    2    3 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 
   2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
   3    4    5    6    7    8    9   10   11   12   13   14   15   16   17 
   4    5    6    7    8    9   10   11   12   13   14   15   16   17   18 
   5    6    7    8    9   10   11   12   13   14   15   16   17   18   19 
   6    7    8    9   10   11   12   13   14   15   16   17   18   19   20 
   7    8    9   10   11   12   13   14   15   16   17   18   19   20   21 
   8    9   10   11   12   13   14   15   16   17   18   19   20   21   22 
   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23 
  10   11   12   13   14   15   16   17   18   19   20   21   22   23   24 
  11   12   13   14   15   16   17   18   19   20   21   22   23   24   25 
  12   13   14   15   16   17   18   19   20   21   22   23   24   25   26 
  13   14   15   16   17   18   19   20   21   22   23   24   25   26   27 
  14   15   16   17   18   19   20   21   22   23   24   25   26   27   28 
  15   16   17   18   19   20   21   22   23   24   25   26   27   28  111

Code:

def Manhattan(x):
    for i in range(n):
        for j in range(n):
            if x[i][j] == 111:
                break
            up = 0
            left = 0
            if x[i-1][j] > 0 and bool(i):
                up = x[i-1][j]
            if x[i][j-1] > 0 and bool(j):
                left = x[i][j-1]

            if bool(up) and bool(left):
                x[i][j] = x[i][j]   min(up, left)   1
            elif bool(up):
                x[i][j] = x[i][j]   up   1
            elif bool(left):
                x[i][j] = x[i][j]   left   1
            else:
                x[i][j] = x[i][j]   1

    for row in x:
        print()
        for val in row:
            print('M' %val, end = " ")
  • Related