Repeat row of a matrix

1 回表示 (過去 30 日間)
dan berkowitz
dan berkowitz 2017 年 10 月 22 日
コメント済み: Cedric 2017 年 10 月 23 日
Hi,
I have a 3x1 array, a = [1 2 3]', and B, a 3x2 matrix.
How can I create C, a 6x2 matrix, whose first row is the same as B and repeated once [a(1)], the second row is the second row of B repeated twice [a(2)], and the third row is the third row of B repeated three times [(a(3)]? I'm trying to use the repelem function but am a bit stuck...Do I have to loop to do this, or is there another way?
Any help would be appreciated. Thx in advance,
DB

採用された回答

Cedric
Cedric 2017 年 10 月 22 日
Here is one way to achieve it. If
>> a = [1;2;3] ;
>> B = randi( 10, 3, 2 )
B =
3 10
6 2
10 10
Then
>> C = arrayfun( @(k)repelem(B(k,:),a(k),1), 1:numel(a), 'UniformOutput', false ) ;
>> C = vertcat( C{:} )
C =
3 10
6 2
6 2
10 10
10 10
10 10
  2 件のコメント
dan berkowitz
dan berkowitz 2017 年 10 月 23 日
thank you!
Cedric
Cedric 2017 年 10 月 23 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by