Write a userdefined MATLAB function that sorts the elements

Write a user-defined MATLAB function that sorts the elements of a vector from smallest to largest. The input to the function should be a vector of any length. Do not use the MATLAB inbuilt sort, max or min functions.

Solution

>> v=[4 2 7 4 12 9];
>> for k1 = 1:length(v)
>> [sv(k1),ix] = min(v); % Find Minimum & Index
>> v(ix) = []; % Set Previous Minimum To Empty
end
>> v = sv

v =

2 4 4 7 9 12

 Write a user-defined MATLAB function that sorts the elements of a vector from smallest to largest. The input to the function should be a vector of any length.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site