Truncate an array along the dimensions specified

Version 1.0.0 (2.58 KB) by wfH
A handy function works as the "inverse" of `padarray`. Truncate the ND array in a concise and intuitive fasion.
3 Downloads
Updated 24 Jun 2023

View License

I used `padarray`, a built-in function in Image Processing Toolbox, very often.
It was a bit tedious and annoying to turn the padded array into its original size, particularly for ND array.
For example, you might write as follow:
`A = B(1+padsize(1):end-padsize(1), 1+padsize(2):end-padsize(2), 1+padsize(3):end-padsize(3), ......, 1+padsize(k):end-padsize(k));`, which looks messy.
Therefore, I created this handy function as the "inverse" of `padarray` to truncate an array in a concise and intuitive fasion.
The usage mimics `padarray`'s:
`B = truncatearray(A, truncatesize)`
`B = truncatearray(A, truncatesize, direction)`
Examples:
A = rand(randperm(randi([4, 10], 1)));
padsize = randperm(randi([0, ndims(A)], 1));
paddirection= 'pre';
B = padarray(A, padsize, paddirection);
C = truncatearray(B, padsize, paddirection);
if isequal(C, A), disp('Pass'), else, disp('Error');end

Cite As

wfH (2024). Truncate an array along the dimensions specified (https://www.mathworks.com/matlabcentral/fileexchange/131558-truncate-an-array-along-the-dimensions-specified), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0