Duplicating rows in a matrix
116 次查看(过去 30 天)
显示 更早的评论
Hi there
I have a matrix that i need to make twice as long. i.e i need to double the length of columns. eg i need:
[0 1 0 0]
[0 2 0 0]
to become:
[0 1 0 0]
[0 1 0 0]
[0 2 0 0]
[0 2 0 0]
Thanks
1 个评论
采纳的回答
Star Strider
2019-1-23
编辑:Star Strider
2019-1-23
Try this:
M = [0 1 0 0
0 2 0 0];
M2 = repmat(M, 1, 2);
M2 = reshape(M2', [], 4)'
producing:
M2 =
0 1 0 0
0 1 0 0
0 2 0 0
0 2 0 0
You could combine both calculations into a one-line calcualtion if you want to.
EDIT —
Generally, for ‘N’ repitions, the code becomes:
N = 4; % Number Of Repititions
Mn = repmat(M, 1, N)
Mn = reshape(Mn', [], 2*N)'
producing:
Mn =
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 2 0 0
0 2 0 0
0 2 0 0
0 2 0 0
3 个评论
Star Strider
2022-1-3
编辑:Star Strider
2022-1-4
@Matthew Allison — Thank you!
@Stephen — Because I wanted to make it general enough to work with earlier versions as well.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!