0
votes

I have two vectors

A = [...] %size 1x320
B = [...] %size 1x192

I would like to combine the two vectors in one but the way I want to combine them is the following:

Take the first 5 elements of vector A then add 3 elements from vector B add the next 5 elements from vector A then add the next element from vector B and so on until the both vectors are combined in one. I think the process should be repeated 64 times since 320/5=64 and 192/3=64.

Is there any built-in Matlab function to do that?

1
See my edit for a second solution that does not involve for loops.Miriam Farber

1 Answers

1
votes

I don't think that there is a built-in function that does exactly that, but the following will do what you want:

A=randi(10,1,320);
B=randi(10,1,192);
C=zeros(1,length(A)+length(B));
for i=1:5
    C(i:8:end)=A(i:5:end);
end
for i=6:8
    C(i:8:end)=B(i-5:3:end);
end

Then the array C is the combined array.

Edit: Another way to do that, without for loops:

A=randi(10,1,320);
B=randi(10,1,192);
A_new=reshape(A,5,[]);
B_new=reshape(B,3,[]);
C=[A_new;B_new];
C=reshape(C,[1,numel(C)]);

In this solution, by specifying the third parameter in reshape(A,5,[]) to be [], we allow it to adjust the number of columns according to the length of A, given that the number of rows in the reshaped array is 5. In addition, numel(C) is the total number of elements in the array C. So this solution can be easily generalized to higher number of arrays as well.