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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site