0

I am trying to visualise the path it took to reach from top left to bottom right. I have calculated updated the cost of each cells.. now trying to visualise the path.

i.e.

[[ 4  4  7 10 13]
 [ 5  7  9 13 13]
 [ 5  9 11 12 12]
 [ 6  7  7  8 12]
 [ 9  7 10  8 10]]

map is the above matrix. Starting from bottom right, I want to check if neighbours is small or equal, then x,y moves to that position. And changing the value to Nan so that it will come up as bad in the visualisation on matplotlib.

But it is throwing "ValueError: cannot convert float NaN to integer".

Can someone please advice on how to solve this?

```

origingrid=np.ones((row,col),dtype=int)*np.nan

#Start backtracking to plot the path  
temp=map.astype(float)
x,y=row-1,col-1
path=[]
#set the lower right cell to be nan.
temp[np.int(x),np.int(y)]=np.nan

while x>0.0 or y>0.0:
  path.append([np.int(x),np.int(y)])
  
  if map[x][y-1] <= map[x][y]:
    origingrid[x,y-1] = np.ravel_multi_index([x,y], (row,col)) 
    xy=np.unravel_index(np.int(origingrid[np.int(x),np.int(y)]), (row,col)) #<--ValueError: cannot convert float NaN to integer
    x,y = xy[0],xy[1]
  else: 
    origingrid[x-1,y] = np.ravel_multi_index([x,y], (row,col)) 
    xy=np.unravel_index(np.int(origingrid[np.int(x),np.int(y)]), (row,col))
    x,y = xy[0],xy[1]

temp[np.int(x),np.int(y)]=np.nan
path.append([np.int(x),np.int(y)])
```
3
  • it contains only a number, not 2 values Commented Dec 30, 2021 at 22:54
  • I only realised that. How do I make it such that x, y is the coordinates of map[x][y-1]? Commented Dec 30, 2021 at 23:03
  • Thank you @drum Commented Dec 30, 2021 at 23:29

1 Answer 1

1

The coordinates of map[x][y - 1] is simply (x, y - 1).

The if-else should read:

  if map[x][y-1] <= map[x][y]:
        x, y = x, y - 1
  else: 
    x, y = x - 1, y
Sign up to request clarification or add additional context in comments.

1 Comment

it worked. Thank u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.