MATLAB: Combination of multiple arrays

combination of multiple arrays

Lets have two arrays of same length, A = [2 4 6 8 10], B = [5 8 3 7 2], I want to know how to have a resultant array which will contain, result = [2 5 4 8 6 3 8 7 10 2]; If the lengths are different, suppose, A = [2 4 6 8 10], B = [5 8 3], so how will I get the result as [2 5 4 8 6 3 8 10];

Best Answer

Consider A has more element than B:
A=2:2:10
B=[5 8 3]
result=zeros(1,numel(A)+numel(B));
Dif=2*numel(B);
dif=numel(A)-numel(B);
result(end:-1:Dif+1)=A(end:-1:numel(A)-dif+1);
A(end:-1:numel(A)-dif+1)=[];
result(1:2:Dif-1)=A;
result(result==0)=B