Compensate the vector with the last entry
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
mingcheng nie
am 25 Apr. 2023
Beantwortet: Steven Lord
am 31 Jul. 2024
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 Kommentare
Stephen23
am 25 Apr. 2023
"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?
Akzeptierte Antwort
Weitere Antworten (1)
Steven Lord
am 31 Jul. 2024
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!