Function Name: knitted Inputs: 2. (double) 1xM vector to be inserted in the odd
ID: 3743913 • Letter: F
Question
Function Name: knitted Inputs: 2. (double) 1xM vector to be inserted in the odd position indices 3. (double) 1xN vector to be inserted in the even position indices Outputs: 2. (double) 1xQ vector with input vectors "knitted" Background: Fall is in the air, so your new wardrobe will no longer do. It's time to trade in all your free FASET t-shirts for some handmade sweaters! Your job is to use MATLAB to knit together two input vectors. Function Description: Given two vectors, reverse the first input vector, sort the second, then output a larger vector where the values in the first vector are inserted in the odd indices and the values in the second vector are inserted in the even indices. If the two vectors are not the same length, the shorter vector should be extended by adding 1s to the end. Example: 2) sweater- knitted( [83, 67, 1, 7, 3, 1], sweater -- [1 1 3 2 7 3 1 4 67 1 83 11Explanation / Answer
-----------------Code---------------------
X1 = [83 67 1 7 3 1];
X2 = [3 4 1 2];
knitted(X1,X2)
function Y = knitted(X1,X2)
M = size(X1)(2);
N = size(X2)(2);
% Reverse X1
X1 = fliplr(X1);
% Sort X2
X2 = sort(X2);
% Append 1s to the end of shorted array
if(M<N)
for i = M+1:N
X1(i) = 1;
end
else
for i = N+1:M
X2(i) = 1;
end
end
% get size of new vectors after appending 1s
M = size(X1)(2);
Y = [];
% Size of the returning array will be 2*M
% so loop from 1 to 2*M
for i = 1:2*M
% for i = 1,3,5 get value from vector X1
% else get value from X2
if(mod(i,2)==1)
Y(i) = X1(floor((i+1)/2));
else
Y(i) = X2(floor((i+1)/2));
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.