Write a function with two inputs: a vector and a scalar; and outputs a vector of
ID: 3564662 • Letter: W
Question
Write a function with two inputs: a vector and a scalar; and outputs a vector of the same size as the input vector. Depending on the value of the scalar, different operations are performed on the vector as follows (use an if statement):
If the scalar is equal to 1, add 2 to the vector and return it as the output
If the scalar is equal to 2, multiply the vector by 2 and return it as the output
If the scalar is any other value, reverse the order of the vector. You may find the reverse function you wrote in Lab 5 useful here.
Write a script (lab6_3.m) to test your function using the following example inputs:
(a) scalar input = 0, vector input = 1: 10
(b) scalar input = 1, vector input = -10: 10
(b) scalar input = 2, vector input = -10: 2: 10
Explanation / Answer
function [outvec]=lab6_3(invec,k)
outvec=0;
if k==1
outvec=invec+2;
return outvec;
elseif k==2
outvec=invec*2;
return outvec;
else
outvec=invec(end:-1:1);
return outvec;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.