Skip to content
Advertisement

how to move specific part of a 2d array to either left right up or down?

I know that you can move an array with NumPy so if you use np.roll you can shift array to right or to the left. I was wondering how to move a specific set of values with in the array to either left right up or down.

for example enter image description here

if I wanted to move what is circled in red to the left how would i be able to move that and nothing else?

Advertisement

Answer

numpy can use slice to get subarray and later assing it in different place

JavaScript

Result:

JavaScript

It keeps original values in [0][1], [1][1]. If you want remove them then you could copy subarray, set zero in original place, and put copy in new place

JavaScript

Result

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement