Increasing size of Multidimensional Array

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

3 件のコメント

teresa
teresa 2015 年 6 月 22 日
Ok, this is my code. Any improvement/correction will be welcomed.
si = size(T);
nd = ndims(T);
ix = cell(nd,1);
for d = 1:nd
ix{d} = 1:si(d);
end
A = zeros(si+1);
A(ix{:}) = T;
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 22 日
Usuaris, have you looked at the two answers?
teresa
teresa 2015 年 6 月 22 日
I haven't! sorry.
Your solution is more simple than mine. Thanks for your answer.

サインインしてコメントする。

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 22 日

2 投票

idx=num2cell(size(T)+1);
T(idx{:})=0;

2 件のコメント

Stephen23
Stephen23 2015 年 6 月 22 日
編集済み: Stephen23 2015 年 6 月 22 日
Very tidy solution. To answer the original question first assign T to a new array S:
S = T;
X = num2cell(1+size(S));
S(X{:}) = 0;
Guillaume
Guillaume 2015 年 6 月 22 日
Indeed! Much simpler (and faster) than mine.

サインインしてコメントする。

Guillaume
Guillaume 2015 年 6 月 22 日

1 投票

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

質問済み:

2015 年 6 月 22 日

コメント済み:

2015 年 6 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by