In: Computer Science
USING MATLAB
Where indicated in the script below, write a function, called
myflip, which accepts one vector v (either a column or a row) and
outputs the same vector v of the same dimensions, but with the
values in reverse order (use MATLAB function flip()). In other
words, v will be overwritten by its flipped version. In your
function, you may only use length() and floor(). You only need one
loop.
%this code tests the function, myflip, which you will write below
v1 = 100*rand(1);
v1 = myflip(v1)
n = randi([2 100], 1, 1);
v2 = 100*rand(1,2*n);
v2 = myflip(v2)
n = randi([2 100], 1, 1);
v3 = 100*rand(2*n+1,1);
v3 = myflip(v3)
myflip.m file code:
function[v]=myflip(v)%% after function keyword return vector is
there after functin nameinput parameter
v=flip(v);%%flipping the vector and storig it in
v
end%% end of function
testmyfilecode.m(just for testing program )
%this code tests the function, myflip, which you will write below
v1 = 100*rand(1)
disp('after myflip')
v1 = myflip(v1)
n = randi([2 100], 1, 1);
v2 = 100*rand(1,2*n)
disp('after myflip')
v2 = myflip(v2)
n = randi([2 100], 1, 1);
v3 = 100*rand(2*n+1,1)
disp('after myflip')
v3 = myflip(v3)
Output
since its not possible to show the full vector v3 iam giving the screenshot of end of v3 and front of flipped v3 so that output can be verified
code screenshot for function file