Zeroing-out array values outside of a centered slice

Viewed 125

I have a 2D array (Sorry about the mess):

[[4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]
 [5.91745 13.7384 35.5878 79.1378 94.6858 60.1723 27.6673 12.7537 5.91745]
 [11.1614 29.0228 108.87 175.518 155.52 138.734 96.2145 29.5323 11.1614]
 [14.7855 59.3563 156.22 410.418 669.983 400.613 163.023 58.3647 14.7855]
 [9.87354 87.0854 124.38 775.761 1630.11 744.488 115.188 90.8995 9.87354]
 [8.47966 64.2404 145.325 475.687 931.189 519.85 122.555 69.1578 8.47966]
 [10.0377 27.1189 106.654 112.141 132.298 233.69 136.029 30.2086 10.0377]
 [6.50091 14.2923 24.318 73.8886 128.941 106.06 48.8712 15.8181 6.50091]
 [4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]]

I know how to take a slice of an array, and zero-out the values of that slice to get something that looks like below (where I've zeroed-out a 3 x 3 slice):

[[4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]
 [5.91745 13.7384 35.5878 79.1378 94.6858 60.1723 27.6673 12.7537 5.91745]
 [11.1614 29.0228 108.87 175.518 155.52 138.734 96.2145 29.5323 11.1614]
 [14.7855 59.3563 156.22 0.0 0.0 0.0 163.023 58.3647 14.7855]
 [9.87354 87.0854 124.38 0.0 0.0 0.0 115.188 90.8995 9.87354]
 [8.47966 64.2404 145.325 0.0 0.0 0.0 122.555 69.1578 8.47966]
 [10.0377 27.1189 106.654 112.141 132.298 233.69 136.029 30.2086 10.0377]
 [6.50091 14.2923 24.318 73.8886 128.941 106.06 48.8712 15.8181 6.50091]
 [4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]]

But how would I go about zeroing-out all the values in the array except for those in the original center slice (like below)?

[[0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 410.418 669.983 400.613 0.0 0.0 0.0]
 [0.0 0.0 0.0 775.761 1630.11 744.488 0.0 0.0 0.0]
 [0.0 0.0 0.0 475.687 931.189 519.85 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]]
1 Answers
Related