How to perform repmat function to repeat rows of a matrix

24 ビュー (過去 30 日間)
Zee
Zee 2022 年 8 月 19 日
回答済み: Bruno Luong 2022 年 8 月 19 日
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
  1 件のコメント
Zee
Zee 2022 年 8 月 19 日
This works, thank you.

サインインしてコメントする。

その他の回答 (5 件)

Torsten
Torsten 2022 年 8 月 19 日
編集済み: Torsten 2022 年 8 月 19 日
B = repelem(A,2,1)
  2 件のコメント
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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by