# Increasing size of Multidimensional Array

8 views (last 30 days)
teresa on 22 Jun 2015
Commented: Guillaume on 22 Jun 2015
Hi all,
I have one multidimensional array T of size (n1,n2,n3,...) and I need to build a new array S of size (n1+1,n2+1,n3+1,...) with the same values of A and zeros in all the added coefficients. In other words, I need to pad one "slice" of zeros at the end of each dimension of T.
The problem is that I don't know the size of T until runtime... so code should work for any number of dimensions...
Any idea how to do it? (other than a extremely slow loop in all coefficients of A) Thanks in advance! Sergio
teresa on 22 Jun 2015
I haven't! sorry.

Azzi Abdelmalek on 22 Jun 2015
idx=num2cell(size(T)+1);
T(idx{:})=0;
##### 2 CommentsShowHide 1 older comment
Guillaume on 22 Jun 2015
Indeed! Much simpler (and faster) than mine.

Guillaume on 22 Jun 2015
newT = zeros(size(T) + 1); %create new array with increased size
destidx = arrayfun(@(s) 1:s, size(T), 'UniformOutput', false);
newT(destidx{:}) = T
The last line is the programmatic equivalent of
newT(1:size(T, 1), 1:size(T, 2), 1:size(T, 3), ...) = T
And the line before that is just building these indices

### Community Treasure Hunt

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

Start Hunting!

Translated by