MATLAB: Combining 2 matrices every other row

combining matricesMATLAB

Hello,
I have two matrices for example A = [1; 2; 3; 4; 5] and B = [A; B; C; D; E]. I would like to combine them in such a way that I get C = [1; A; 2; B; 3; C; 4; D; 5; E].
Can you please help me with this task? Thank you so much ?

Best Answer

Hi,
try:
A = [-1; -3; -5; -7; -9]
B = [2; 4; 6; 8; 10]
result = reshape(([A B])',[],1)
gives:
A =
-1
-3
-5
-7
-9
B =
2
4
6
8
10
result =
-1
2
-3
4
-5
6
-7
8
-9
10
or with symbolic variables:
syms a b c d e
A = [1; 2; 3; 4; 5];
B = [a; b; c; d; e];
result = reshape(([A B].'),[],1)
results in:
A =
1
2
3
4
5
B =
a
b
c
d
e
result =
1
a
2
b
3
c
4
d
5
e
Best regards
Stephan