how can I get successive maximum value from this code?
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!