MATLAB question For a given vector a 7 4 2 19 5 8 2 please s
MATLAB question
For a given vector a=[ 7 -4 2 19 5 8 2], please swap the neighboring two numbers to have the larger one on the right side through an external function.
Solution
Matlab function code:
function y=larger_right(x)
i=length(x)
y = zeros(1, i); % Pre-allocate!
for ii = 1:i
[value, index] = min(x);
y(ii) = value;
x(index) = Inf;
end
a=[ 7 -4 2 19 5 8 2]
larger_right(a)
ans =
-4 2 2 5 7 8 19
![MATLAB question For a given vector a=[ 7 -4 2 19 5 8 2], please swap the neighboring two numbers to have the larger one on the right side through an external fu MATLAB question For a given vector a=[ 7 -4 2 19 5 8 2], please swap the neighboring two numbers to have the larger one on the right side through an external fu](/WebImages/6/matlab-question-for-a-given-vector-a-7-4-2-19-5-8-2-please-s-986140-1761506648-0.webp)