how to duplicate each cell in an array

Hi, i have an array of numbers. I want a that a different array will have each cell in the first array but multiple times. for example: array1 = [1,2,5] so array2 = [1,1,1,2,2,2,5,5,5] if i choose to multiply by 3. I have a function that can do it
factor = 3
for i=1:length(array1)
array2(1+factor*(i-1):factor*i) = array1(i);
end
however i was wondering if there is a function that does it which will be less time costly since I have to repeat this process multiple times with large arrays.
thanks!

 採用された回答

KL
KL 2017 年 12 月 11 日
編集済み: KL 2017 年 12 月 11 日

0 投票

Something like this maybe,
n = 3;
array1 = [1,2,5];
array2 = reshape(array1.*ones(n),1,[])
1 1 1 2 2 2 5 5 5
or
array2 = ones(n).*array1
array2 = array2(:).'
or
array2 = reshape(repmat(array1,n,1),1,[])

1 件のコメント

TZ
TZ 2017 年 12 月 11 日
Great thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

質問済み:

TZ
2017 年 12 月 11 日

コメント済み:

TZ
2017 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by