recurMedian

recurMedian - %the recursive vectors are empty out = ;...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function out = recurMedian(vec) %Given a 1xN array of scalars (a vector), write a function recurMedian %that recursively calculates the median of the input array. For a sorted %array with an odd number of elements, the median is defined as the %middle element of the array. For a sorted array with an even number of %elements, the median is defined as the average of the two middle %elements of the array. Your function should return this median. Return %an empty vector if the input is empty. %usage: out = recurMedian(vec) function out = slowlySort(vec) %Given a 1xN array of scalars (a vector), write a function slowSort %that recursively sorts the vector in ascending order and outputs this %sorted vector. %usage: out = slowSort(vec) if isempty(vec) %initial condition to check and see if the original vector and all of
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %the recursive vectors are empty out = ; %terminating condition; sets the output equal to an empty vector if vec %is an empty vector else out = sort(vec); %sorts the given vector end end vec = slowlySort(vec); %calls the slowSort function to put the vector into ascending order if isempty(vec) %terminating condition out = ; %output elseif length(vec)==2 %accounts for if the length of the vector is 2 numbers out = sum(vec)./2; %finds the average of the two numbers elseif length(vec)==1 %accounts for it the length of the vector is 1 number out = vec; %assigns the output to that value else out = recurMedian(vec(2:end-1)); %calls the original function and eliminates the first and last elements %each time the function is called (moving toward terminating condition end e end...
View Full Document

Ask a homework question - tutors are online