How to perform repmat function to repeat rows of a matrix

34 次查看(过去 30 天)
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

更多回答(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 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by