0

I want to combine

A = [1 3 5 7 9];  
B = [2 4 6 8 10];

into

C = [1 2 3 4 5 6 7 8 9 10];

I made an interlacing for-loop like this:

for i=1:length(A)
    C(2*i-1) = A(i);
    C(2*i) = B(i);
end

Is there a better way to interleave than this? perhaps without doing any loop?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
micropyre
  • 103
  • 4
  • https://www.mathworks.com/matlabcentral/answers/476044-interleaving-vectors-in-matlab#answer_387472 – Paolo Jul 03 '20 at 09:16
  • Welcome to Stack Overflow! Please read the tag excerpts of [tag:matlab] and [tag:octave]. Those languages are similar but definitely not the same. Only use both to tag a question when explicitly asking about differences/similarities between the two languages. – Adriaan Jul 03 '20 at 09:16
  • @Paolo thank you! That (:) trick was really helpful. – micropyre Jul 03 '20 at 09:36
  • @Adriaan oops, sorry for using both tags. And thank you for sharing a relevant question. (I initially tried searching for the answer but the one you shared did not pop up from the relevant results) – micropyre Jul 03 '20 at 09:37

0 Answers0