how can I get successive maximum value from this code?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

David Hill
David Hill il 23 Feb 2020
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 Commento
Mohmmad Abu Yousuf
Mohmmad Abu Yousuf il 23 Feb 2020
Modificato: Mohmmad Abu Yousuf il 23 Feb 2020
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by