필터 지우기
필터 지우기

findMax function returning the last value of an xlsread array

조회 수: 3 (최근 30일)
Lillian Lau
Lillian Lau 2018년 7월 26일
답변: Walter Roberson 2018년 7월 26일
Hello, I wrote a findMax function to find the maximum value of all the values stored in an array. I am reading an xlsx file into the array and then using findMax function on it. However, it keeps returning the last value of the array. Here's my code:
function max = findMax(input_array)
L = length(input_array);
for i = 1:L-1
temp = input_array(i);
nextVal = input_array(i+1);
if temp >= nextVal
max = temp;
else
max = nextVal;
end
end
end
Where am I going wrong? Would appreciate some help, and thanks in advance!
  댓글 수: 1
madhan ravi
madhan ravi 2018년 7월 26일
Check the max documentation there is already an inbuilt function in matlab as max.

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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 7월 26일
When i becomes L-1, you will set max to either input_array(L-1) or to input_array(L), ignoring everything else you know about max to that point.
You should be initializing max to -inf, and comparing the elements one by one, and if they are greater than the previous max then make the value the new max. You never need to look at the adjacent value when you use this strategy.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by