フィルターのクリア

Compensate the vector with the last entry

15 ビュー (過去 30 日間)
mingcheng nie
mingcheng nie 2023 年 4 月 25 日
回答済み: Steven Lord 2024 年 7 月 31 日 15:29
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
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?
mingcheng nie
mingcheng nie 2024 年 7 月 31 日 11:36
sorry for the ambiguity. I have a loop around 10^4 times, where within each loop I will need compensate the vector :)

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2023 年 4 月 25 日
V = [2,4,7,3];
K = 8;
V(end+1:K) = V(end)
V = 1×8
2 4 7 3 3 3 3 3

その他の回答 (1 件)

Steven Lord
Steven Lord 2024 年 7 月 31 日 15:29
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]
x = 1x4
2 4 7 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = paddata(x, 7, Side = 'trailing', Pattern = 'edge')
y = 1x7
2 4 7 3 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or to show FillValue with a different value:
y = paddata(x, 7, Side = 'trailing', FillValue = -999)
y = 1x7
2 4 7 3 -999 -999 -999
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by