(HELP)Who knows anything about MATLAB?
#1
TECH Regular
Thread Starter
iTrader: (9)
Join Date: Jul 2008
Location: Knoxville, TN
Posts: 432
Likes: 0
Received 0 Likes
on
0 Posts
(HELP)Who knows anything about MATLAB?
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
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
#2
On The Tree
Join Date: Jul 2007
Location: Kansas City, Kansas
Posts: 101
Likes: 0
Received 0 Likes
on
0 Posts
This sounds like a HW assignment. Why else would you not be aloud to use the max command.
function Q1 = mymax[list]
%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), % I think you want list(ii) not x(ii), bc you do not have a variable x.
% 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 % This just ends the if statement.
for list(1) < list(i) % were you wanting a nested for for loop? What is i?
x_max = list(i)
if list(x) % You havent declared a variable x.
error('wrong number of inputs')
i=0;
return;
end
if % No parameters for the if statement
error('the input isnt a one row matrix')
i=0;
return;
end Your errors could be because Im not sure if the varibles are passing outside of the of the for loops. I havent taken a MATLAB class since last spring so I dont remember. The variables might need to be declared as global.
Sorry if nothing I added helped. I am by no means a MATLAB expert.
Is there any more info about the problem?
Could you post your mymax function? Edit: this is the mymax function.
Use the MATLAB help.
function Q1 = mymax[list]
%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), % I think you want list(ii) not x(ii), bc you do not have a variable x.
% 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 % This just ends the if statement.
for list(1) < list(i) % were you wanting a nested for for loop? What is i?
x_max = list(i)
if list(x) % You havent declared a variable x.
error('wrong number of inputs')
i=0;
return;
end
if % No parameters for the if statement
error('the input isnt a one row matrix')
i=0;
return;
end
Sorry if nothing I added helped. I am by no means a MATLAB expert.
Is there any more info about the problem?
Could you post your mymax function? Edit: this is the mymax function.
Use the MATLAB help.
Last edited by MikeMoss; 02-16-2010 at 11:57 AM.
#4
TECH Fanatic
iTrader: (16)
This sounds like a HW assignment. Why else would you not be aloud to use the max command.
Your errors could be because Im not sure if the varibles are passing outside of the of the for loops. I havent taken a MATLAB class since last spring so I dont remember. The variables might need to be declared as global.
Sorry if nothing I added helped. I am by no means a MATLAB expert.
Is there any more info about the problem?
Could you post your mymax function? Edit: this is the mymax function.
Use the MATLAB help.
Your errors could be because Im not sure if the varibles are passing outside of the of the for loops. I havent taken a MATLAB class since last spring so I dont remember. The variables might need to be declared as global.
Sorry if nothing I added helped. I am by no means a MATLAB expert.
Is there any more info about the problem?
Could you post your mymax function? Edit: this is the mymax function.
Use the MATLAB help.