How can I find the smallest element in an array without using the function 'min'??
이전 댓글 표시
for example I need to find the minimum element in array [1 2 4 3 0 -1 8 9 -2] but without using the function min.
I cannot also use sort.
댓글 수: 1
Stephen23
2018년 12월 26일
>> X = [1,2,4,3,0,-1,8,9,-2];
>> V = unique(X);
>> V(1)
ans = -2
채택된 답변
추가 답변 (1개)
madhan ravi
2018년 12월 25일
X = [1,2,4,3,0,-1,-8,9,-2];
for i =1:numel(X)-1
if X(i)>X(i+1)
min=X(i+1);
else
continue
end
end
댓글 수: 2
Image Analyst
2018년 12월 26일
Not a good idea to use "min" as the name of a variable because it's an important built-in function. And you also forgot to initialize it, so the code will fail if x has all the same values. Andrei's answer is the way to do it.
madhan ravi
2018년 12월 26일
Yes sir thank you :) , missed that part.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!