How to generate an array alternating the values of two others?

So, I have two arrays (I made them simple for the example)
A = [1 2 3 4 5 6]; B = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
The array I wish to generate is [1 10 2 20 3 30 4 40 5 50 6 60 1 70 2 80 3 90 ...]
Any idea of how to proceed? For now, I'm trying on something like this:
Sequence = []; for i = 1: length(B) j=% ??? range 1:length(A) i=1 j=1; ... i=7 j=1; i=8 j=2; ... 1=n j=??? Sequence = [Sequence A(1,j)]; Sequence = [Sequence B(1,i)]; end

답변 (3개)

Jos (10584)
Jos (10584) 2014년 5월 22일
A = [1 2 3 4 5 6];
B = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
% in steps
N = max(numel(A),numel(B))
ia = 1+rem(0:N-1, numel(A))
ib = 1+rem(0:N-1, numel(B))
C = [A(ia) ; B(ib)]
C = reshape(C,1,[])
% in a one-liner
C2 = reshape([A(1+rem(0:max(numel(A),numel(B))-1, numel(A))) ; B(1+rem(0:max(numel(A),numel(B))-1, numel(B)))],1,[])
George Papazafeiropoulos
George Papazafeiropoulos 2014년 5월 22일
편집: George Papazafeiropoulos 2014년 5월 22일

1 개 추천

A = [1 2 3 4 5 6];
B = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
M1=[A,A,A(1:3)];
M2=B;
M=[M1;M2];
Sequence=M(:)'
Andrei Bobrov
Andrei Bobrov 2014년 5월 22일
out0 = [A(rem(0:numel(B)-1,numel(A))+1);B];
out = out0(:)';

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 5월 22일

댓글:

2014년 5월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by