# How to get the highest value from the latest samples?

1 ビュー (過去 30 日間)
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 件のコメント表示非表示 1 件の古いコメント
Luccas S. 2021 年 12 月 10 日
Yes, exactly that!

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

### 採用された回答

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.
% Put into an equation to get p.
end
##### 5 件のコメント表示非表示 4 件の古いコメント
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!

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

### カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by