how can I get successive maximum value from this code?

1 次查看(过去 30 天)
Hi all, Could you please help me to get the successive value from this code?
Here, I want to get the maximum value of from 2 values. e.g. when it is 5th iteration (i = 5) that time the maximum value should be 80 because when it was i = 3 that time we got the max value 60 from i = 1 and it is remained at i = 2.
More specifically what I want to get from this codes,
when it is i = 1
B = max(40,60) = 60
when i = 2
B = max (60,30) = 60
when i = 3
B = max (60,80) = 80
when i = 4
B = max(80,70) = 80
when i = 5
B = max(80,50) = 80
when i = 6
B = max(80,90) = 90
when i = 7
B = max(90,30) = 90
But in this code I am getting something different at i = 5 which is 70 but I need to get 80 at this time.
Thank you in advance.
A = [40, 60, 30, 80, 70, 50, 90, 30];
for i = 1:length(A)-1
B = max(A(i:i+1));
C = A(i+1);
if C/B < 0.7
x = C/10;
else
x = C/5;
end
end

采纳的回答

David Hill
David Hill 2020-2-23
Why not index B and x? Otherwise, you are overriding their values each time the loop is run. For i =5, max ([70 50]) = 70! Not sure why you thing it should be 80.
A = [40, 60, 30, 80, 70, 50, 90, 30];
for i = 1:length(A)-1
B(i) = max(A(i:i+1));
C = A(i+1);
if C/B(i) < 0.7
x(i) = C/10;
else
x(i) = C/5;
end
end
  1 个评论
Mohmmad Abu Yousuf
Mohmmad Abu Yousuf 2020-2-23
编辑:Mohmmad Abu Yousuf 2020-2-23
Thank you Sir I got it for index term.
and the other one is,
for every max value it will take the previous max value and the current value of i.
I mean for i = 5, previous maximum value is 80 and the current value is 50 so the current maxmum value should be 80.
At the end of simulation the B matrix should be like this,
B = [60,60,80,80,80,90,90]
How can I edit this code to get likewise Sir.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by