My assignment is to create a function that takes a vector of the form
v = [3 9 1 8 5 4]
and puts it in order from smallest to largest as in
v = [1 3 4 5 8 9]
So far, I've got this, but it only works on a vector of the form v = [a b].
Code:
function x = putinorder(p)
a = length(p);
for i=1:a-1
if p(a) < p(a-i)
ex = p(a-i);
p(a-i) = p(a);
p(a) = ex;
end
x = p;
end
end
>> putinorder([5,1])
ans =
1 5
I am required to use a for loop. Any suggestions or tips? Thanks,
Marc