Interleave Write a function interleave to interleave two row arrays of equal len
ID: 3591261 • Letter: I
Question
Interleave Write a function interleave to interleave two row arrays of equal length. For example for the row arrays arrayOne and array Two, the output row array is array Three - [arrayOne(1), arrayTwo(1), arrayOne(2) ,arrayTwo1(2),. The function should work for rows of any length. Hint: Create a new array by concatinating arrayOne and array Two (the details of what type of concatenation is needed, you need to determine), flatten the array, and to finally end up with the desired row array. Restrictions You may not use a for or a while loop. For example,: arrayone = [2, 4, 81; arrayTwo- [3, 9, 27]; will produce arrayThree =12, 3, 4, 9, 8, 27] Your Function save CReset MATLAB Documentation 1 function [ arrayThree ] = interleave( arrayone, arrayTwo ) % Your code goes here % 3 4 s endExplanation / Answer
interleave.m------------------------------------>
function [ arrayThree ] = interleave( arrayOne,arrayTwo )
[M,N] = size(arrayOne);
[P,Q] = size(arrayTwo);
if(M>1 && N>1)
error('ArrayOne must be a vector (1xM or Mx1)');
end
if(P>1 && Q>1)
error('arrayTwo must be a vector (1xM or Mx1)');
end
if(isempty(arrayOne) && isempty(arrayTwo))
arrayThree = []; return;
elseif(isempty(arrayOne))
arrayThree = arrayTwo; return;
elseif(isempty(arrayTwo))
arrayThree = arrayOne; return;
end
if(M==1)
colvec = false;
else
colvec = true;
end
if(length(arrayOne)<length(arrayTwo))
tmp=arrayOne;
arrayOne=arrayTwo;
arrayTwo=tmp;
swtch = true;
else
swtch = false;
end
len = length(arrayOne) + length(arrayTwo);
arrayThree = zeros(1,len);
if(~swtch)
idy = 2:2:2*length(arrayTwo);
lasty = idy(end);
idx = 1:2:lasty;
idx = [idx lasty+1:len];
else
idy = 1:2:2*length(arrayTwo);
lasty = idy(end);
idx = 2:2:lasty;
idx = [idx lasty+1:len];
end
arrayThree(idy) = arrayTwo;
arrayThree(idx) = arrayOne;
if(colvec)
arrayThree=arrayThree(:);
end
end
Output :-
>> arrayOne=[2 4 8]
arrayOne =
2 4 8
>> arrayTwo=[3 9 27]
arrayTwo =
3 9 27
>> interleave(arrayOne,arrayTwo)
ans =
2 3 4 9 8 27
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.