MATLAB: How to crop arrays using a vector? (reverse of padarray)

arraycropimcroppadarray

*Short version: I'm looking for the reverse of padarray, where you can do padarray(a,[2 2 2]), but instead have it crop [2 2 2] instead of pad [2 2 2]. (Or alternatively, imcrop for more then two dimensions); *
Long version: I have a cell arrays of unknown size K, each cell in the array contains an numerical array with dimensions (m,n,k,… )importantly:
1. I don't know beforehand how many dimensions there will be for the
numerical arrays in the different cell arrays, but I
know that within a cell array all the numerical arrays will have
the same number of dimensions.
2. While all the arrays have the same number of dimensions, they
differ in size.
Basically I want to make a functions that crops the numerical arrays to the smallest dimensions, and then concatenates them, i.e. if
a{1} = zeros(10,10)
a{2} = zeros(16,6)
a{3} = zeros(6,6)
I want the outcome to be an array c with size (6,6,3).
I guess the problem is that I don't know how to index the arrays for a variable amount of dimensions. That is, I can get the size to which I need to crop (or the number by which I need to crop) in vector form like [6 6] or [15 4 8], but I don't know how to use this vector to index my arrays.
Thanks in advance!
Edit: Just thought of something, but it's very ugly: if we know we want to crop to an array of size (30,30,30)
minSize = [30 30 30]
c = []
for ii = length(a);
temp = a{ii}(:);
mask = zeros(minSize)
padding = (minSize - size(a{ii})./2;
mask = padarray(mask,padding,1,'both');
mask = logical(mask);
temp(mask)= [];
temp = reshape(temp,minSize);
c = cat(ndim(a{ii})+1,c,temp);
end

Best Answer

So something along the lines of:
x = rand(10,6,8,9);
nd = ndims(x);
c = repmat({':'},nd-1,1);
for ii = 1:nd;
x = shiftdim(x,1);
x = x(3:end-2,c{:});
end