Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fucntion HELP!

Subject: Fucntion HELP!

From: Sam mayton

Date: 14 Feb, 2010 01:02:05

Message: 1 of 3

Question: Write a function named mymax that takes as input a list of numbers and returns the maximum value in the list and its location (index). You may NOT use Matlab's max command. Your function should show an error message and return 0 and 0 if it is called with the wrong number of inputs or if the input isn't a one row matrix.

What I have so far:

function Q1=mymax[list]
%input a list of values
%output-maximum value
%output-value position
x_max=list(1)
% sets the initial value of the list to the maximum value
for ii = 1:length(list)
 % runs the if statement below for the entire list of numbers
  if x_max < x(ii)
% if statement which checks whether the current value is higher than the next value
x_max = x(ii)
% if x(3) is higher than x(1), and so on, then x_max will become the third value.
end
for list(1)< list(i)
    x_max=list(i)
if list(x)
    error('wrong number of inputs')
    i=0;
    return;
end
if
    error('the input isnt a one row matrix')
    i=0;
    return;
end

Subject: Fucntion HELP!

From: Walter Roberson

Date: 14 Feb, 2010 04:23:15

Message: 2 of 3

Sam mayton wrote:
> Question: Write a function named mymax that takes as input a list of
> numbers and returns the maximum value in the list and its location
> (index).

That's obvious homework, but you clearly showed that you have made a
serious attempt so I will help. In future, though, please be more
specific about the problem you are encountering.

> function Q1=mymax[list]

In Mathlab, in order to return a value from the function, the value must
be assigned to one of the variable names on the left-hand side of the
'=' in the function statement. You never assign anything to Q1 .

Also, the syntax for passing in a value to the function is to put the
variable name in () in the 'function' statement, not in [] .

Your code also doesn't check to see whether the input variable exists
before you try to use it. Hint: error checking of the input should
always occur *before* you use the input.

> %input a list of values
> %output-maximum value
> %output-value position
> x_max=list(1) % sets the initial value of the list to the maximum value
> for ii = 1:length(list) % runs the if statement below for the entire
> list of numbers
> if x_max < x(ii) % if statement which checks whether the current value
> is higher than the next value
> x_max = x(ii) % if x(3) is higher than x(1), and so on, then x_max will
> become the third value.
> end
> for list(1)< list(i)
> x_max=list(i)
> if list(x)
> error('wrong number of inputs')
> i=0;
> return;
> end
> if
> error('the input isnt a one row matrix')
> i=0;
> return;
> end

Subject: Fucntion HELP!

From: Jeff

Date: 14 Feb, 2010 04:54:05

Message: 3 of 3

"Sam mayton" <smayton@utk.edu> wrote in message <hl7i2c$9kt$1@fred.mathworks.com>...
> Question: Write a function named mymax that takes as input a list of numbers and returns the maximum value in the list and its location (index). You may NOT use Matlab's max command. Your function should show an error message and return 0 and 0 if it is called with the wrong number of inputs or if the input isn't a one row matrix.
>
> What I have so far:
>
> function Q1=mymax[list]
> %input a list of values
> %output-maximum value
> %output-value position
> x_max=list(1)
> % sets the initial value of the list to the maximum value
> for ii = 1:length(list)
> % runs the if statement below for the entire list of numbers
> if x_max < x(ii)
> % if statement which checks whether the current value is higher than the next value
> x_max = x(ii)
> % if x(3) is higher than x(1), and so on, then x_max will become the third value.
> end
> for list(1)< list(i)
> x_max=list(i)
> if list(x)
> error('wrong number of inputs')
> i=0;
> return;
> end
> if
> error('the input isnt a one row matrix')
> i=0;
> return;
> end

I'm always ready to help a fellow student when I can (which is rare), so:

In addition to what Walter said, the logic in your first loop is find for finding the max, but not its index. Try saving the index value in that loop at the same time you set the x-max value. I think combined we have given you enough to finish the assignment.

Good luck.
-Jeff

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us