how to write a function to find max & min value in array

hi every one I really need your help in this question write a function m.file to find the maximum and minimum number in array *do not use the built in functions in matlab* ? i know how to find it but i don't know how to make a function ?? could anyone help me ?

댓글 수: 2

Proper answer needed
Since this is likely a homework assignment, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
If you're asking for a "proper answer" to the question as originally asked, that question is impossible since the relational operators like > and <= are built-in functions in MATLAB.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 7월 10일
try it
function [ma mi] = alifun(A)
aw = A(:);
ma = aw(1);
mi = aw(1);
for i1 = 2:numel(aw)
if ma < aw(i1), ma = aw(i1);
elseif mi > aw(i1), mi = aw(i1);
end
end
end

추가 답변 (3개)

Paulo Silva
Paulo Silva 2011년 7월 10일

0 개 추천

After you watch those nice video tutorials about functions and read the documentation try to do your function and if you have any specific question please ask and we will be here to help you.

댓글 수: 5

ahmed  ali
ahmed ali 2011년 7월 10일
편집: DGM 2023년 2월 27일
thank you , but is it the right way to write the function like this ??
function[maximum,minimum]=array(a)
maximum=array(1);
minimum=array(1);
for i=1:length(Array)
if Array(i)>Maximum
Maximum=Array(a);
end
if Array(i)<Minimum
Minimum= Array(a);
end
end
disp('The Masximum is: ')
Maximum
disp('The Minimum is: ')
Minimum
end
ahmed  ali
ahmed ali 2011년 7월 10일
이동: DGM 2023년 2월 27일
still there's error if i run the programme !
-___-
Image Analyst
Image Analyst 2011년 7월 10일
이동: DGM 2023년 2월 27일
You wrote the function incorrectly. You don't want this to be a recursive function so don't call array() inside of array(). You should have used a() instead of array() because a is the matrix while array is your function's name. Also note that MATLAB is case sensitive so Array and array are different.
Image Analyst
Image Analyst 2011년 7월 10일
이동: DGM 2023년 2월 27일
Also note that you used the wrong index, "a" instead of i. It should be
if Array(i)>Maximum
Maximum=Array(i);
end
or better yet, don't use i and j as indexes at all since they are the imaginary constants.
ahmed  ali
ahmed ali 2011년 7월 10일
이동: DGM 2023년 2월 27일
thank you , i understand know =)

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

Dave Correa
Dave Correa 2011년 7월 10일
Hello;
You can to use the following code;:
example:
>> array=magic(8)
>> [maximum,minimun]=find_maxmin(array)
maximun =
64
minimun =
1
Regards,
Dave Correa
function [maximum,minimun]=find_maxmin(array)
maximun=max(max(array))
minimun=min(min(array))

댓글 수: 2

I think that this would be the simplest way to define a search function for maximum and minimum of a data matrix.
regards
Dave
Yes, except this was a homework problem where his instructor said not to use built in functions like max and min.

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

Mohammed Raihan
Mohammed Raihan 2021년 3월 7일
편집: DGM 2023년 2월 27일
function[Max, Min]=minimax(M)
Max=max(M')-min(M');
maxa=max(M');
maxa2=max(maxa);
mini=min(M');
mini2=min(mini);
Min=maxa2-mini2;
end

댓글 수: 1

This does not meet the requirements of the original question. The original question requires that min() and max() aren't used.

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

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2011년 7월 10일

댓글:

DGM
2023년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by