Truncate an array along the dimensions specified

版本 1.0.0 (2.6 KB) 作者: wfH
A handy function works as the "inverse" of `padarray`. Truncate the ND array in a concise and intuitive fasion.
4.0 次下载
更新时间 2023/6/24

查看许可证

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

引用格式

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. 检索来源 .

MATLAB 版本兼容性
创建方式 R2023a
兼容任何版本
平台兼容性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0