Repeat vectors within a matrix multiple times

9 Ansichten (letzte 30 Tage)
samee Rehman
samee Rehman am 14 Jul. 2013
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!

Antworten (3)

Azzi Abdelmalek
Azzi Abdelmalek am 14 Jul. 2013
Bearbeitet: Azzi Abdelmalek am 14 Jul. 2013
n=3
out=reshape(repmat(x',n,1),size(x,2),[])'

Andrei Bobrov
Andrei Bobrov am 14 Jul. 2013
Bearbeitet: Andrei Bobrov am 14 Jul. 2013
x(ones(n,1)*(1:size(x,1)),:);
or
x(kron(1:size(x,1),ones(1,n)),:);

Jan
Jan am 14 Jul. 2013
Bearbeitet: Jan am 14 Jul. 2013
n = 2;
kron(x, ones(n, 1))

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by