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

조회 수: 1 (최근 30일)
Nicholas Colburn
Nicholas Colburn 2013년 2월 20일
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
Jan
Jan 2013년 2월 21일
편집: Jan 2013년 2월 21일
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 2013년 2월 21일
편집: Nicholas Colburn 2013년 2월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2013년 2월 20일
Hint:
if strcmp(SortAlg, 'InsertionSort')
  댓글 수: 4
Nicholas Colburn
Nicholas Colburn 2013년 2월 21일
It would be: FindRunningTime(MergeSort) %or InsertionSort

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by