How to calculate maximum up to current point?

조회 수: 1 (최근 30일)
Steven Niggebrugge
Steven Niggebrugge 2017년 8월 18일
댓글: John D'Errico 2017년 8월 18일
Hi, how can i calculate the an array of current maxima, i.e. for each point in the array to calculate the maximum from the first observation of the array to the current? e.g. for array [3 8 10 6] i want the output to be [3 8 10 6] ?
Is there a function to do this (movmax i think it works for a fixed lookback or lookforward) or do i just need to loop through all observations?
  댓글 수: 2
José-Luis
José-Luis 2017년 8월 18일
You probably meant [3,8,10,10] as your result.
Steven Niggebrugge
Steven Niggebrugge 2017년 8월 18일
yes indeed, thanks very much!

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2017년 8월 18일
cummax([3 8 10 6])
ans =
3 8 10 10
  댓글 수: 2
José-Luis
José-Luis 2017년 8월 18일
편집: José-Luis 2017년 8월 18일
+1
Whenever I think of a tortured way, there's a built-in function. It is hard to keep up.
John D'Errico
John D'Errico 2017년 8월 18일
I keep learning new things on Answers. New little goodies that appeared when I was not watching. I guess we all should read the release notes religiously. That would take all the fun out of it though. :)

댓글을 달려면 로그인하십시오.


José-Luis
José-Luis 2017년 8월 18일
a = [3,8,10,6,5,11,12,-20];
n = numel(a);
result = tril(bsxfun(@minus,a,a'));
[~,idx] = max(result,[],2);
result = a(idx)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by