How to Implement a Custom Max Function in MATLAB?

  • Thread starter Thread starter BMY61
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion focuses on implementing a custom maximum function in MATLAB, named MyMax, which identifies the maximum value from a vector using a FOR loop and an IF statement. Participants emphasize the importance of understanding MATLAB user-defined functions and recommend consulting the MathWorks documentation for guidance. The suggested testing method involves inputting ten integer values and printing the maximum value using the fprintf function. Key resources include the "Working with M-Files" and "M-File Scripts and Functions" documentation pages.

PREREQUISITES
  • Basic understanding of MATLAB syntax and structure
  • Familiarity with user-defined functions in MATLAB
  • Knowledge of control flow statements, specifically FOR loops and IF statements
  • Experience with MATLAB's input and output functions, particularly input and fprintf
NEXT STEPS
  • Read the "Working with M-Files" section on the MathWorks documentation
  • Explore the "M-File Scripts and Functions" documentation for deeper insights
  • Practice writing user-defined functions in MATLAB with various input types
  • Test and debug MATLAB functions using the MATLAB editor and command window
USEFUL FOR

Students learning MATLAB programming, educators teaching MATLAB concepts, and developers creating custom functions for data analysis in MATLAB.

BMY61
Messages
3
Reaction score
0

Homework Statement



Consider how you would pick the maximum value from a list (vector) of values. You take the 1st value in the vector and compare the next value to it. If the next value is larger, then select the next value as the max. You would continue like that moving on to the next value, next value, etc, until the end of vector is reached.

Write a user-written-function in MatLab (name that function as MyMax) to select maximum value from a vector which is given to MyMax as its input argument. Use a FOR loop and an IF statement inside the loop. Please test it by running it on MatLab for several different input value sets. Give the printout of the program to the instructor. Do NOT use any other MatLab function, except length, size, fprintf, and input

To test it, use the following:
A = input (‘ give 10 integer values ‘);
maxA = MyMax(A);
fprintf(‘Max val = %f\n’, maxA);



Homework Equations





The Attempt at a Solution



i have no clue how to create a user written function for this problem, does anyone have any ideas?
 
Physics news on Phys.org
I'd highly recommend you start by reading through the following page on the Mathworks MATLAB documentation site ("Working with M-Files"):
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-41453.html

Once you've got that down, go to the next page over, "M-File Scripts and Functions" to figure out the distinction between scripts (code blocks that help you avoid typing pages of MATLAB commands, may or may not assume that you've already done a bunch of stuff) and functions (code blocks that you run with specific inputs and outputs):
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html

You probably want a function for this task, but don't you have a lab or seminar (or even class time, if you happen to be an engineering student) to cover MATLAB basics?
 
Last edited by a moderator:

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K