Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Combine two equal sized vectors element wise in MatLab?

Tags:

vector

matlab

I have two vectors:

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

That I need to combine together element wise. Meaning that I need the first element of vector a, then the first element of vector b, second of a, second of b, and so forth until I get the following:

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

How do I do this within MatLab?

Edit

I ran a test of the top three answers (Josh, Marc, & Kronos) and compared the time it took to run them. I ran each 100 times after doing a 10 iteration warmup. The vectors created were exactly the same size in length (16e+6) and were random values ranging from 1 to 100:

Test Results
Test:           Total Time (100 runs):      Avg Time Per Exec:
Josh B          21.3687                     0.2137
Marc C          21.4273                     0.2143
Kronos          31.1897                     0.3119

It appears that both Josh's and Marc's solutions are similar in execution time.

like image 647
James Mertz Avatar asked May 20 '13 19:05

James Mertz


People also ask

How do you add vectors together in MATLAB?

Add Row and Column VectorsCreate a 1-by-2 row vector and 3-by-1 column vector and add them. The result is a 3-by-2 matrix, where each (i,j) element in the matrix is equal to a (j) + b(i) : a = [ a 1 a 2 ] , b = [ b 1 b 2 b 3 ] , a + b = [ a 1 + b 1 a 2 + b 1 a 1 + b 2 a 2 + b 2 a 1 + b 3 a 2 + b 3 ] .

How do you concatenate elements in MATLAB?

You can use the square bracket operator [] to concatenate or append arrays. For example, [A,B] and [A B] concatenates arrays A and B horizontally, and [A; B] concatenates them vertically.

How do I concatenate column vectors in MATLAB?

C = vertcat( A1,A2,…,An ) concatenates A1 , A2 , … , An vertically. vertcat is equivalent to using square brackets to vertically concatenate or append arrays. For example, [A; B] is the same as vertcat(A,B) when A and B are compatible arrays.


2 Answers

a = [1 3 5 7 9];
b = [2 4 6 8 10];
temp = [a; b];
combined = temp(:)';
like image 85
Joshua Barr Avatar answered Oct 31 '22 18:10

Joshua Barr


This can be done by the following:

a = [1 3 5 7 9];
b = [2 4 6 8 10];
combinedSize = size(a, 2) * 2;

combined(1:2:combinedSize) = a;
combined(2:2:combinedSize) = b;

This is obviously assuming that your vectors are exactly the same size. If by chance you want to merge two vectors that are not the same size then you can do the following:

combinedSize = max(size(a, 2), size(b, 2)) * 2;
combined = NaN(1,combinedSize);
combined(1:2:size(a,2)*2) = a;
combined(2:2:size(b,2)*2) = b;

This will place a NaN for the remaining elements of the smaller vector. For example, given the following sample vectors:

a = [1 3 5 7 9 11];
b = [2 4 6 8];

will result in the combined vector:

combined =

     1     2     3     4     5     6     7     8     9   NaN    11   NaN
like image 29
James Mertz Avatar answered Oct 31 '22 18:10

James Mertz