How to repeat rows of matrix?
조회 수: 3 (최근 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!