Can someone do this in MATLab(please dont post a picture)? Problem-4: Functions
ID: 2267903 • Letter: C
Question
Can someone do this in MATLab(please dont post a picture)?
Problem-4: Functions Write a function called vectorzip(x,y) that will take as inputs two vectors and return another vector as follows: >>> zipped= vectorzip (x, >>zipped y); >>> a= [7, 5, 9, 10]; >>> zipped= vectorzip (a, b); >>>zipped (7, 4), (5, 8)1 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
clc
clear all
x=[1, 2, 3,4,7];
y=[4,5,6,4];
zipped=vectorzip(x,y);
fprintf('zipped = [')
for i=1:length(zipped)
fprintf('(%d,%d)',zipped(i,1),zipped(i,2))
if(i~=length(zipped))
fprintf(' , ')
end
end
fprintf('] ')
---------------------------------------------------------------
The vectorzip function is:::
---------------------------------------------------------------
function zipped=vectorzip(x,y)
p=length(x);
q=length(y);
if(p>q)
m=q;
else
m=p;
end
zipped=[];
for i=1:m
zipped=[zipped; x(i),y(i)];
end
Output:
zipped = [(1,4) , (2,5) , (3,6) , (4,4)]