Interleaved repmat (row duplication)

조회 수: 7 (최근 30일)
Albert
Albert 2021년 11월 5일
댓글: Albert 2021년 11월 5일
I would like to do a specific repmat such that if I have:
a=[1 0 0;0 0 1;1 1 1]
a = 3×3
1 0 0 0 0 1 1 1 1
I would like to duplicate each row by a value N, so that in the case N = 2 each row will be duplicated twice:
b=[1 0 0;1 0 0;0 0 1;0 0 1;1 1 1;1 1 1]
b = 6×3
1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1
Is there an easy way of doing this interleaved repmat? Thanks

채택된 답변

Stephen23
Stephen23 2021년 11월 5일
The simple and efficient approach is to use REPELEM:
a = [1,0,0;0,0,1;1,1,1]
a = 3×3
1 0 0 0 0 1 1 1 1
b = repelem(a,2,1)
b = 6×3
1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1
  댓글 수: 1
Albert
Albert 2021년 11월 5일
Very elegant solution indeed! I found another option which is:
a = [1,0,0;0,0,1;1,1,1];
reshape(repmat(a',2,1),size(a,2),size(a,1)*2)'
ans = 6×3
1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1
But yours is much better. thanks!

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

추가 답변 (1개)

Sudharsana Iyengar
Sudharsana Iyengar 2021년 11월 5일
Try this
A=[1,0,0; 0 0 1; 1 1 1;];
k=1;
for i =1:3
T(k:k+1,:)=repmat(A(i,:),2,1);
k=k+2;
end
T
T = 6×3
1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 1 1

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by