Please help me vectorize my loop to construct query vector from sample points

1 回表示 (過去 30 日間)
Yi-xiao Liu
Yi-xiao Liu 2021 年 4 月 13 日
コメント済み: Yi-xiao Liu 2021 年 4 月 13 日
I want to construct a vector xq (the query vector) from xs (sample point vector) for use in interp1. Ideally, xq would:
  1. contain all element from xs
  2. evenly spaced when between any xs(i) and xs(i+1)
  3. xq(i+1)-xq(i)<t always true for a given threshold t.
  4. for xs(i+1)-xs(i)<t already, no xq between the 2 points (but just on xs(i+1) and xs(i))
I cannot think of any code to achieve all of 4 without some kind of loop. Does anyone have a good idea? Thanks in advance
Here is a loop version I wrote. I am sorry if I didn't prase my original question clearly.
t=2;
xs=cumsum(10*rand(1,10))
xsdelta=diff(xs)
xq=[];
for i=1:length(xsdelta)
xq=[xq,xs(i),nan([1,floor(xsdelta(i)/t)])];
end
xq=[xq,xs(end)]
xq=fillmissing(xq,"linear",2)

採用された回答

Bruno Luong
Bruno Luong 2021 年 4 月 13 日
編集済み: Bruno Luong 2021 年 4 月 13 日
xs=cumsum(ceil(10*rand(1,10)))
t=pi;
p=cumsum([0 ceil(diff(xs)/t)]);
xq=interp1(p,xs,0:p(end))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by