Hi Could anyone help suggest a faster way to construct this vector?
xval = [1 2 7 9];
numX = [1 4 2 3];
% I want final output to be [1 2 2 2 2 7 7 9 9 9];
fullX = [];
for runi=1:length( xval)
tval = xval( runi);
n = numX( runi);
if n > 0
fullX= [ fullX; repmat( tval, n, 1)];
end
end %

 채택된 답변

Star Strider
Star Strider 2018년 9월 17일

0 개 추천

If you have R2015a or later, use the repelem (link) function:
fullX = repelem(xval, numX)

댓글 수: 2

Pete sherer
Pete sherer 2018년 9월 17일
Thank you verymuch
Star Strider
Star Strider 2018년 9월 17일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 9월 17일

댓글:

2018년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by