MATLAB: Multidimensional colon operator

array indexingpermute

I want to do something like that below: copy an N-dimensional array into a N+1-dimensional array at index t in the last dimension. Can this be done efficiently and cleaner than umpteen if-tests or nasty permutes?
function A = insert_x_into_A(A,x,t)
if ndims(A) == 2
A(:,t) = x;
elseif ndims(A) == 3
A(:,:,t) = x;
elseif ndims(A) == 4
A(:,:,:,t) = x;
...
end
I think that something like the pseudo-code below would be neat
function A = insert_x_into_A(A,x,t)
A(colon(ndims(x)),t) = x;

Best Answer

Two approaches: 1. Reshape the matrix at first:
function A = insert_x_into_A(A,x,t)
siz = size(A);
A = reshape(A, [], siz(end));
A(:, t) = x(:);
A = reshape(A, siz);
2. Use a cell as index vector: EDITED: Cleaned up with Bruno's suggestion.
function A = insert_x_into_A(A,x,t)
idx(1:ndims(A) - 1) = {':'};
A(idx{:},t) = x;