フィルターのクリア

Array indexing for moving IQR

2 ビュー (過去 30 日間)
Anna McCann
Anna McCann 2018 年 3 月 11 日
回答済み: Walter Roberson 2018 年 3 月 12 日
I'm calculating a moving iqr, similar to the movmean, movmad, and movstd functions, discarding the endpoints:
A = [4 8 6 -1 -2 -3 -1 3 4 5];
for i = 1:length(A)-k+1
M(i) = iqr(A(i:i+k-1)
end
M = [3.0000 6.7500 6.0000 1.5000 1.5000 4.5000 3.7500 1.5000]
M is as expected. How can I do this more efficiently using array indexing?

回答 (2 件)

Jos (10584)
Jos (10584) 2018 年 3 月 12 日
When N (= number of elements in A) and K (= number of elements to use) are not too large you could create an intermediate (N-K+1)-by-K array. This could be (slightly) more efficient than a for-loop but also looks more cryptic ...
A = [4 8 6 -1 -2 -3 -1 3 4 5]
K = 3
N = numel(A)-K+1
I = cumsum([1:N ; ones(K-1,N)], 1)
M = iqr(A(I), 1)
  1 件のコメント
Anna McCann
Anna McCann 2018 年 3 月 12 日
Okay thanks, I'll try profiling it and see if it speeds it up.

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


Walter Roberson
Walter Roberson 2018 年 3 月 12 日
R2016b and later,
iqr(A((1:k).'+(0:length(A)-k)))

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by