How to perform repmat function to repeat rows of a matrix

A=[1,0,0,0,1;2,0,0,0,2;3,0,0,0,3]
I would like to repeat each row for n times and get output something like this when n is 2:
output=[1,0,0,0,1;1,0,0,0,1;2,0,0,0,2;2,0,0,0,2;3,0,0,0,3;3,0,0,0,3]

 Respuesta aceptada

A=[1,0,0,0,1;2,0,0,0,2;3,0,0,0,3]
A = 3×5
1 0 0 0 1 2 0 0 0 2 3 0 0 0 3
A(repmat(1:end,2,1),:)
ans = 6×5
1 0 0 0 1 1 0 0 0 1 2 0 0 0 2 2 0 0 0 2 3 0 0 0 3 3 0 0 0 3

2 comentarios

This works, thank you.
Marco Caputano
Marco Caputano el 25 de Sept. de 2025
Editada: Marco Caputano el 25 de Sept. de 2025
indeed that works! it took me some time to get it, however thanks! :-D

Iniciar sesión para comentar.

Más respuestas (5)

Torsten
Torsten el 19 de Ag. de 2022
Editada: Torsten el 19 de Ag. de 2022
B = repelem(A,2,1)

2 comentarios

Hi, cannot use this function since it got introduced in 2015 version, I am using 2011 version. Thanks.
A = [1,0,0,0,1;2,0,0,0,2;3,0,0,0,3];
n = 2;
B = [];
for i = 1:size(A,1)
B = [B;repmat(A(i,:),n,1)];
end
B
B = 6×5
1 0 0 0 1 1 0 0 0 1 2 0 0 0 2 2 0 0 0 2 3 0 0 0 3 3 0 0 0 3

Iniciar sesión para comentar.

A=[1,0,0,0,1;2,0,0,0,2;3,0,0,0,3]
A = 3×5
1 0 0 0 1 2 0 0 0 2 3 0 0 0 3
reshape(repmat(reshape(A,1,1,[]),2,1,1),[],size(A,2))
ans = 6×5
1 0 0 0 1 1 0 0 0 1 2 0 0 0 2 2 0 0 0 2 3 0 0 0 3 3 0 0 0 3

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2011b

Etiquetas

Preguntada:

Zee
el 19 de Ag. de 2022

Editada:

el 25 de Sept. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by