Repeat vectors within a matrix multiple times
14 ビュー (過去 30 日間)
古いコメントを表示
Hi, I am looking for an efficient approach (without a for loop) to repeat a vector multiple times within a matrix.
For e.g. x = [1 2; 3 4; 5 6]
I want to repeat each row n times:
if n = 2: x = [1 2; 1 2; 3 4; 3 4; 5 6; 5 6]. The important point is that the repeated rows should be together.
I was thinking of using repmat, but that allows me to repeat the matrix and not vectors within the matrix..as far as I know, any help would be great!
0 件のコメント
回答 (3 件)
Azzi Abdelmalek
2013 年 7 月 14 日
編集済み: Azzi Abdelmalek
2013 年 7 月 14 日
n=3
out=reshape(repmat(x',n,1),size(x,2),[])'
0 件のコメント
Andrei Bobrov
2013 年 7 月 14 日
編集済み: Andrei Bobrov
2013 年 7 月 14 日
x(ones(n,1)*(1:size(x,1)),:);
or
x(kron(1:size(x,1),ones(1,n)),:);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!