How to perform repmat function to repeat rows of a matrix

조회 수: 26 (최근 30일)
Zee
Zee 2022년 8월 19일
편집: Marco Caputano 2025년 9월 25일 10:33
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]

채택된 답변

Bruno Luong
Bruno Luong 2022년 8월 19일
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
Zee
Zee 2022년 8월 19일
This works, thank you.
Marco Caputano
Marco Caputano 2025년 9월 25일 8:49
편집: Marco Caputano 2025년 9월 25일 10:33
indeed that works! it took me some time to get it, however thanks! :-D

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Torsten
Torsten 2022년 8월 19일
편집: Torsten 2022년 8월 19일
B = repelem(A,2,1)
  댓글 수: 2
Zee
Zee 2022년 8월 19일
Hi, cannot use this function since it got introduced in 2015 version, I am using 2011 version. Thanks.
Torsten
Torsten 2022년 8월 19일
편집: Torsten 2022년 8월 19일
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

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2022년 8월 19일
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

Bruno Luong
Bruno Luong 2022년 8월 19일
n = 2;
A(ceil((1:n*end)/n),:)

Bruno Luong
Bruno Luong 2022년 8월 19일
n = 2;
A(kron(1:end,ones(1,n)),:)

Bruno Luong
Bruno Luong 2022년 8월 19일
n = 2;
kron(A,ones(n,1))

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by