-2

I have the following array and I would like to expand an area around X and turn array elements around X from 0 to 1. Any suggestions on how to do it?

From this array

[[0,0,0,0,0,0,0],
 [0,0,0,x,0,0,0],
 [0,0,0,0,0,0,0]]

To this array

[[0,0,1,1,1,0,0],
 [0,0,1,x,1,0,0],
 [0,0,1,1,1,0,0]]
azro
  • 53,056
  • 7
  • 34
  • 70
Kiwi
  • 173
  • 2
  • 9

2 Answers2

0

Not the optimal solution, but works for me. I use ROS and nav_msgs/OccupancyGrid message:

    def draw_thickened_grid_point(self, index):
        indexes_arround = []
        indexes_arround.append(index + 1)
        indexes_arround.append(index - 1)
        indexes_arround.append(index + self._map_width)
        indexes_arround.append(index - self._map_width)
        indexes_arround.append(index + self._map_width+1)
        indexes_arround.append(index + self._map_width-1)
        indexes_arround.append(index - self._map_width+1)
        indexes_arround.append(index - self._map_width-1)
        self._grid.data[int(index)] = 100
        for ind in indexes_arround:
            if(self._grid.data[int(ind)] == 0):
                self._grid.data[int(ind)] = 30
Lukashou-AGH
  • 125
  • 10
-1

Here a possible solution, it could be improved. I am really interested to see a better implemenentation.

for i in range(len(a)):
    for j in range(len(a[i])):
            if (a[i][j] =="x"):

                if(len(a)>i+1):
                    a[i+1][j] =1
                    if(len(a[i])>j+1):
                        a[i+1][j+1] =1
                    if(len(a[i])>j-1):
                        a[i+1][j-1] =1

                if(len(a)>i-1):
                    a[i-1][j] =1
                    if(len(a[i])>j+1):
                        a[i-1][j+1] =1  
                    if(len(a[i])>j-1):
                        a[i-1][j-1] =1
                if(len(a[i])>j+1):
                        a[i][j+1] =1
                if(len(a[i])>j-1):
                        a[i][j-1] =1

print(a)

ragnar
  • 150
  • 1
  • 6
  • This is almost certainly not the best solution. – AMC Nov 11 '19 at 21:57
  • As I said, it could be improved. Still better than yours ;) – ragnar Nov 12 '19 at 15:52
  • I haven’t written a solution because OP’s post is woefully lacking in detail and I do not wish to encourage extremely low quality questions. Also, is the array element actually the string `’x’`, or is `x` just a variable? – AMC Nov 12 '19 at 15:54