Why am I getting a "Not enough input arguments." error in my function?

Not really familiar with MATLAB but I have a couple of sorting functions and would like to compare their running times with the function FindRunningTime:
function t = FindRunningTime(SortAlg)
n = 10000.*randn(1,20);
if strcmp(SortAlg, 'InsertionSort')
tic;
InsertionSort(n);
t = toc;
else
tic;
MergeSort(n);
t = toc;
end %if
end %FindRunningTime
where SortAlg is specified as merge or insert. However when running the code I get the error "Not enough input arguments." on line 2 of both sorting algs. Hopefully someone could point me in the right direction. Also if anyone knows of a nice way to specify the random number generators range, that would be helpful. Below is the code for both sorting algorithms:
function list = MergeSort(list)
if numel(list) <= 1
return
else
middle = ceil(numel(list) / 2);
left = list(1:middle);
right = list(middle+1:end);
left = sort(left);
right = sort(right);
if left(end) <= right(1)
list = [left right];
return
end
%merge(left,right)
counter = 1;
%index placeholder for final list
while (numel(left) > 0) && (numel(right) > 0)
if(left(1) <= right(1))
list(counter) = left(1);
left(1) = [];
else
list(counter) = right(1);
right(1) = [];
end
counter = counter + 1;
end
if numel(left) > 0
list(counter:end) = left;
elseif numel(right) > 0
list(counter:end) = right;
end
%end merge
end %if
end %MergeSort
function list = InsertionSort(list)
for i = (2:numel(list))
value = list(i);
j = i - 1;
while (j >= 1) && (list(j) > value)
list(j+1) = list(j);
j = j-1;
end %while
list(j+1) = value;
end %for
end %InsertionSort

2 Kommentare

Jan
Jan am 21 Feb. 2013
Bearbeitet: Jan am 21 Feb. 2013
The line 2 of MergeSort is empty, so I do not believe that it causes an error. Please post the complete error message also instead of describing it.
What do you mean by "specify the random number generators range"?
Nicholas Colburn
Nicholas Colburn am 21 Feb. 2013
Bearbeitet: Nicholas Colburn am 21 Feb. 2013
Apologies, that would be line 3 of both sorts. Here is the error, it's the same for both; there isn't much to it:
"Error using InsertionSort (line 3) Not enough input arguments."
As far as your second question, I would like to generate a sequence of numbers between 0 and 10,000 using (I'm assuming) some form of the rand() function.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Hint:
if strcmp(SortAlg, 'InsertionSort')

4 Kommentare

Thanks for pointing that out! I wasn't quite sure how to compare the two, but unfortunately it still doesn't solve the input argument error.
Exactly what are you passing as the parameter when you run FindRunningTime ? Show the command line or the line of code.
It would be: FindRunningTime(MergeSort) %or InsertionSort

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 20 Feb. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by