Compensate the vector with the last entry
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a length L vector contains some numbers, I want to compensate this vector to length K, where K > L, with repeating the last entry of the vector. For example, the vector is [2 4 7 3], after compensate, it will be [2 4 7 3 3 3 3 3]. I hope there is an efficient way to do so because I actually have more than 10^4 vectors to compensate.
Thanks,
댓글 수: 2
Stephen23
2023년 4월 25일
"I hope there is an efficient way to do so because I actually have more than 10^4 vectors to compensate."
Do you really have 1e4 separate vectors stored in the workspace? How did you get them all there?
채택된 답변
추가 답변 (1개)
Steven Lord
2024년 7월 31일
If you were using release R2023b or later, you could use the paddata function with the Side name-value argument and either the FillValue name-value argument or the Pattern name-value argument with the 'edge' pattern.
x = [2 4 7 3]
y = paddata(x, 7, Side = 'trailing', Pattern = 'edge')
Or to show FillValue with a different value:
y = paddata(x, 7, Side = 'trailing', FillValue = -999)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!