How to repeat rows of matrix?
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a matrix and a vector indicating how many times the corresponding lines in the matrix should appear. I'd like to turn this into a single matrix.
For example:
M= 1 2 V=1
2 3 2
4 3 1
2 4 2
should become
M =1 2
2 3
2 3
4 3
2 4
2 4
Does anyone have advice on how to accomplish this? Many thanks.
0 个评论
采纳的回答
Matt Kindig
2013-6-13
编辑:Matt Kindig
2013-6-13
One way:
f = @(k) repmat(M(k,:), round(V(k)), 1);
MM = cell2mat(arrayfun(f, (1:length(V))', 'UniformOutput', false));
0 个评论
更多回答(4 个)
Roger Stafford
2013-6-13
Here's yet another possibility:
p = accumarray(cumsum([1;v]),1);
M = M(cumsum(p(1:end-1)),:);
0 个评论
Azzi Abdelmalek
2013-6-13
out=cell2mat(arrayfun(@(x) repmat(M(x,:),V(x),1),[1:numel(V)]','un',0))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!