Rolling max/min maximum and minimum

This may have already been asked and aswered, but I couldn't find anything on Matlab Answers or Google. Is there a slick vectorized way to calculate a rolling maximum/minimum for a vector. So the maximum over the previous n values (inclusive of the current value). For indices i < n the value should either be the max of the values with indices 1:i or NaN, I can live with either. So for example:
x = [1, 3, 2, 7, 4, 3]; n = 2;
should give
rollingMax = [NaN, 3, 3, 7, 7, 4] or [1, 3, 3, 7, 7, 4]

回答 (3 件)

Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日

0 投票

If you have the Image Processing Toolbox, you can use IMDILATE
>> imdilate( [1, 3, 2, 7, 4, 3] ,[1,1])
ans =
1 3 3 7 7 4

1 件のコメント

Chris
Chris 2012 年 11 月 27 日
Don't have Imagine Processing Toobox unfortunately....

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

Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日

0 投票

2 件のコメント

Arthur
Arthur 2012 年 11 月 27 日
Matt J
Matt J 2012 年 11 月 27 日
Yes, but it's accordingly not very fast :-(

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

José-Luis
José-Luis 2012 年 11 月 27 日
編集済み: José-Luis 2012 年 11 月 27 日

0 投票

Without a toolbox:
x = randi(10,1,100);
n = 2;
maxVec = arrayfun(@(a,b) max(x(a:b)),1:numel(x)-n+1,n:numel(x));

1 件のコメント

Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日
That's not really vectorized, since we know arrayfun underperforms speed-wise as compared to for-loops.

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB Mobile Fundamentals についてさらに検索

タグ

質問済み:

2012 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by