Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem-4: Functions Write a function called vectorzip(x,y) that will take as in

ID: 2247964 • Letter: P

Question

Problem-4: Functions

Write a function called vectorzip(x,y) that will take as inputs two vectors and return another vector as follows:

>>> x = [1, 2, 3];

>>> y = [4, 5, 6];

>>> zipped = vectorzip(x, y);

>>> zipped

[(1, 4), (2, 5), (3, 6)]

>>> a = [7, 5, 9, 10];

>>> b = [4, 8];

>>> zipped = vectorzip (a, b);

>>> zipped

[(7, 4), (5, 8)]

Note that normally, you would need to have an error check in your code to check if your input is a vector or not. For this question, we will assume that the user will always enter the proper inputs.

Explanation / Answer

function op = vectorzip(x,y)

l_x = length(x)
l_y = length(y)
l_op = min(l_x,l_y);
  
if l_x != l_y
x = x(1:l_op);
y = y(1:l_op);
end
  
op = [ x.' y.' ];     
end;

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote