Combination of multiple arrays
Show older comments
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];
Accepted Answer
More Answers (1)
Faster, much simpler code, uses less memory, creates only one intermediate variable (a scalar value), and works regardless of which vector is longer. For two row vectors A and B, all you need is:
>> A = [2,4,6,8,10];
>> B = [5,8,3];
>> N = min(numel(A),numel(B));
>> C = [reshape([A(1:N);B(1:N)],1,[]),A(N+1:end),B(N+1:end)]
C =
2 5 4 8 6 3 8 10
1 Comment
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!