필터 지우기
필터 지우기

Maximum in For Loop

조회 수: 1 (최근 30일)
m13
m13 2016년 10월 30일
답변: Image Analyst 2016년 10월 30일
I need to find the maximum value of the last equation in my for loop as well as the iterative value. How would I do this? For example I need to find the maximum value of z and for what value of x.
for x = 0:10
y = 3*x;
z = sqrt(y/2);
end

답변 (1개)

Image Analyst
Image Analyst 2016년 10월 30일
Try this:
zMax = -inf;
xAtZMax = 0;
for x = 0:10
y = 3*x;
z = sqrt(y/2);
if z > zMax
% New max found.
zMax = z;
xAtZMax = x;
end
end
fprintf('The max z is %f that occurs at x = %d\n', zMax, xAtZMax);

카테고리

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