How to get the highest value from the latest samples?

조회 수: 5 (최근 30일)
Luccas S.
Luccas S. 2021년 12월 10일
댓글: Luccas S. 2021년 12월 10일
I have code that calculates an error for each sample, and I would like to always get the highest value from the last samples.
error(n,1)=Ia(n)-Ia_future(n,1)
p(n,1) = (1+0.2)*max(error(n-1,1))
I want to get the maximum value of the last n samples of error(n-1,1) that were calculated. The "max" above there is just to demonstrate what I'm trying to say.
  댓글 수: 2
Rik
Rik 2021년 12월 10일
You mean like max(error((end+1-n):end,1))?
Other than that, you should really not use error as a variable name.
Luccas S.
Luccas S. 2021년 12월 10일
Yes, exactly that!

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

채택된 답변

Image Analyst
Image Analyst 2021년 12월 10일
error is a built-in function so don't call your variable that. Call it errors or differences instead.
Is this what you mean:
Ia_future = rand(5, 3) % Create sample data.
Ia = rand(5, 1)
% Initialize/preallocate p.
p = zeros(size(Ia));
[rows, columns] = size(Ia_future)
for n = 1 : rows % For every row...
% We're now on iteration "n".
% Subtract the "last" n values,
% meaning the values we've seen "so far"
% which would be the values from 1 to n.
errorsSoFar = Ia(1:n) - Ia_future(1:n,1)
% Put into an equation to get p.
p(n) = (1+0.2)*max(errorsSoFar)
end
  댓글 수: 5
Image Analyst
Image Analyst 2021년 12월 10일
So that would be case 1, and I gave code for that. But you didn't seem to try it. Why not?
Luccas S.
Luccas S. 2021년 12월 10일
I just tested it, it worked.
I was thinking that I should disregard the n-1 value of the first sample but it wasn't necessary. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by