Max vector find without function max

조회 수: 1 (최근 30일)
Lubos Plechac
Lubos Plechac 2021년 10월 21일
댓글: Bryant Lash 2021년 10월 21일
I have problem with finding max number in vector without max function, I know that I can use max but without that?
Normally I will do that B = max(X) , I dont know how to limit end , because I use X(i+1) and I can't find more numbers that are not in the vector.
X = [0 1 1 2 3 3 3 5]
My bad solution how to start?
for i = 1:length(X) - 1
if X(i) < X(i+1)
else X(i) > X(length(X+1))
end
end

답변 (1개)

Bryant Lash
Bryant Lash 2021년 10월 21일
So I'm not clear why you don't want to use the max function, but alright! Here's a solution:
X = [0 1 1 2 3 3 3 5]
currMax = -inf;
for i = 1:length(X)
if X(i) > currMax;currMax = X(i); end;
end
disp(currMax);
  댓글 수: 2
James Tursa
James Tursa 2021년 10월 21일
"... I'm not clear why you don't want to use the max function ..."
Because this is obviously homework.
Bryant Lash
Bryant Lash 2021년 10월 21일
Fair point, I don't love just solving people's homework

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by