Duplicate elements in array n times, where n is an array itself

7 ビュー (過去 30 日間)
Oliver
Oliver 2016 年 9 月 2 日
コメント済み: Oliver 2016 年 9 月 2 日
Hi everyone,
I have two arrays of the same length and would like to duplicate each element in the first array n times, where n is the value in the second array. Example:
A = [1 2 3]
B = [2 3 1]
then C shall be = [1 1 2 2 2 3]
How can this be done? I checked the forum and tried to play around with repmat but could not get it to work.
Any help is greatly appreciated! Thanks!
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 9 月 2 日
You mean how to duplicate (not to multiply)

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

採用された回答

Stephen23
Stephen23 2016 年 9 月 2 日
If you have MATLAB 2015a or more recent, then use repelem:
>> repelem([1 2 3],[2 3 1])
ans =
1 1 2 2 2 3
  1 件のコメント
Oliver
Oliver 2016 年 9 月 2 日
I do and thanks that works like a charm!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 9 月 2 日
A = [1 2 3]
B = [2 3 1]
C=cell2mat(arrayfun(@(x,y) repmat(x,1,y),A,B,'un',0))

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by