Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Remove padding from an array
| ResourceFunction["ArrayCrop"][array] removes rows or columns of zeros from the top, bottom, left or right of an array. | |
| ResourceFunction["ArrayCrop"][array,pad] removes rows or columns of pad from the top, bottom, left or right of an array. | 
Remove the zero padding around an array:
| In[1]:= | ![array = {{0, 0, 1}, {1, 1, 1}, {0, 1, 0}}; padded = CenterArray[array, {7, 7}];
ResourceFunction["ArrayCrop"][padded]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/5e337cba9f356f0d.png) | 
| Out[1]= |  | 
Remove the zero padding around a more complicated array:
| In[2]:= | ![array = Map[Sqrt, RandomInteger[{-4, 4}, {3, 4}]]; padded = PadLeft[PadRight[array, {4, 5}], {9, 7}];
ResourceFunction["ArrayCrop"][padded]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/58701be57dfc74e0.png) | 
| Out[2]= |  | 
| In[3]:= |  | 
| Out[3]= |  | 
Create a symbolic array and pad it with zeros:
| In[4]:= | ![array = Array[C, {5, 4}];
padded = ArrayPad[array, {{4, 3}, {3, 4}}]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/56b7c7dec9286cd1.png) | 
| Out[4]= |  | 
Remove the zero padding:
| In[5]:= | ![ResourceFunction["ArrayCrop"][padded]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/7cb57fcee5896ede.png) | 
| Out[5]= |  | 
Create an array padded by xs:
| In[6]:= | ![array = {{0, 0, 1}, {1, 1, 1}, {0, 1, 0}};
padded = CenterArray[array, {7, 7}, x]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/78443ad29769c46a.png) | 
| Out[6]= |  | 
Remove the padding:
| In[7]:= | ![ResourceFunction["ArrayCrop"][padded, x]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/7b6542dc7fb7dfb8.png) | 
| Out[7]= |  | 
Create a symbolic rank-3 array and pad it with zeros:
| In[8]:= | ![array = Array[C, {5, 4, 3}];
Short[padded = ArrayPad[array, {{4, 3}, {3, 4}, {2, 3}}]]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/4a6ad8ca6edde2c9.png) | 
| Out[8]= |  | 
Remove the padding:
| In[9]:= | ![ResourceFunction["ArrayCrop"][padded]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/1879272be87c97bf.png) | 
| Out[9]= |  | 
If everything is padding, nothing is removed:
| In[10]:= | ![array = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; padded = CenterArray[array, {7, 7}];
ResourceFunction["ArrayCrop"][padded]](https://www.wolframcloud.com/obj/resourcesystem/images/363/36361a5f-29bc-4255-90b9-3ddf892e97fa/3ed83598c16a25ab.png) | 
| Out[11]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License