Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
ahmed  ali
ahmed ali am 10 Jul. 2011
Kommentiert: DGM am 27 Feb. 2023
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 Kommentare
Hakim
Hakim am 18 Aug. 2022
Proper answer needed
Steven Lord
Steven Lord am 18 Aug. 2022
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 10 Jul. 2011
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

Weitere Antworten (3)

Paulo Silva
Paulo Silva am 10 Jul. 2011
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 Kommentare
Image Analyst
Image Analyst am 10 Jul. 2011
Verschoben: DGM am 27 Feb. 2023
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 am 10 Jul. 2011
Verschoben: DGM am 27 Feb. 2023
thank you , i understand know =)

Melden Sie sich an, um zu kommentieren.


Dave Correa
Dave Correa am 10 Jul. 2011
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 Kommentare
Dave Correa
Dave Correa am 10 Jul. 2011
I think that this would be the simplest way to define a search function for maximum and minimum of a data matrix.
regards
Dave
Image Analyst
Image Analyst am 10 Jul. 2011
Yes, except this was a homework problem where his instructor said not to use built in functions like max and min.

Melden Sie sich an, um zu kommentieren.


Mohammed Raihan
Mohammed Raihan am 7 Mär. 2021
Bearbeitet: DGM am 27 Feb. 2023
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 Kommentar
DGM
DGM am 27 Feb. 2023
This does not meet the requirements of the original question. The original question requires that min() and max() aren't used.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by