Concatenate different length vectors - matlab

I am having difficulty concatenating vectors in MATLAB.
A = [1
2
3]
B = [6
7
8
9
10]
Desired result:
C = [1
2
3
6
7
8
9
10]
where the sizes of A and B are different in every iteration of my script and I want to form the concatenated resulting vector, C, which has a dynamic size.
This is what I have tried:

A = [1
2
3];
B = [6
7
8
9
10];
Vertical concatenation of two vectors/matrices is what you want, done like this...
C = [A; B];
... or this...
C = [A
B];
... or this...
C = vertcat(A,B);
All three of these give
C = [1
2
3
6
7
8
9
10]
% As you requested...
You were running into trouble because you were trying to use horzcat
C = horzcat(A',B');
Horizontal concatenation merges matrices horizontally, i.e.
C = [1, 6
2, 7
3, 8
?, 9
?, 10]
So to avoid this, you've transposed the matrices to make them rows instead of columns, then transposed the result back?? You just need vertcat! I have shown the shorthand and full form for this above.

Try:
A = [1 2 3];
B = [4 5 6 7 8 9 10];
C = [A B]
For vertical vectors A' and B' use:
C = [A;B]

The fool-proof way is this:
C = [A(:);B(:)];
If you use this method then it does not matter if A and B are row vectors, column vectors, or even matrices.

Related

Finding the mean of more than one vector

I am tring to get the mean of three vectors but the mean function is not working.
Example:
A = [1 2 3 4 5];
B = [2 3 4 5 6];
C = [3 4 5 6 7];
V = mean(A,B,C); % should be [2 3 4 5 6] as each column is the some of the same column in A, B and C divided by three.
Any Help?
For a general case (row or column vector case), you can use this -
mean_vals = mean(cat(2,A(:),B(:),C(:)),2)

Got confused with a vector indexed by a matrix, in Matlab

The following codes runs in Matlab:
a = [1 2 3 4]
b = [ 1 2 3; 1 2 3; 1 2 3]
a(b)
The result of a(b) is a matrix:
[ 1 2 3; 1 2 3; 1 2 3]
Can anyone explain what happened here? Why a vector can be indexed by a matrix, how to interpret the result?
That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).
For example, what you have there is a vector of a = [1 2 3 4]. When you try to use b to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and what you are doing is creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.
Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:
A = [1 2
3 4
5 6
7 8]
A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.
Here are some examples to further your understanding. Let's define a matrix A such that:
A = [5 1 3
7 8 0
4 6 2]
Here is some MATLAB code to strengthen your understanding for this kind of indexing:
A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
In order to be complete, let's do this for a vector:
V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.
Notice that the return value of a(b) is the same size as b.
a(b) simply takes each element of b, call it b(i,j), as an index and returns the outputs a(b(i,j)) as a matrix the same size as b. You should play around with other examples to get a more intuitive feel for this:
b = [4 4 4; 4 4 4];
a(b) % Will return [4 4 4; 4 4 4]
c = [5; 5];
a(c) % Will error as 5 is out of a's index range

How do I Combine two equal sized vectors element wise in 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.
a = [1 3 5 7 9];
b = [2 4 6 8 10];
temp = [a; b];
combined = temp(:)';
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
Place the vectors below eachother in a matrix and use reshape. For example:
>> A=[1 2 3]
A =
1 2 3
>> B=[4 5 6]
B =
4 5 6
>> C=reshape([A;B],1,size(A,2)+size(B,2))
C =
1 4 2 5 3 6
It's straightforward to generalize to more than 2 vectors.
You can also give a try to looping, for example:
a=[1 2 3 4 5];
b=[11 12 13 14 15];
for i = 1:N
{
if (i%2==0)
{ c[i] = b[i]; }
else
{ c[i] = a[i]; }
This shall work!
All the answers above only work if the two vectors have the same number of elements. The following will work even if they have different number of elements:
>>
A = [1 3 5];
B = [2 4 6 7 8];
C = [1 3 5 7 8];
D = [2 4 6];
AB = nan(1,2*max(numel(A),numel(B)));
CD = nan(1,2*max(numel(C),numel(D)));
AB(2*(1:length(A))) = A;
AB(1+2*(1:length(B))) = B;
CD(2*(1:length(C))) = C;
CD(1+2*(1:length(D))) = D;
>>
AB = AB(~isnan(AB))
CD = CD(~isnan(CD))
The result would be:
AB =
1 2 3 4 5 6 7 8
CD =
1 2 3 4 5 6 7 8

What does A=[x; y'] in Matlab mean?

I'm learning Matlab and I see a line that I don't understand:
A=[x; y']
What does it mean? ' usually means the transponate but I don't know what ; means in the vector. Can you help me?
The [ ] indicates create a matrix.
The ; indicates that the first vector is on the first line, and that the second one is on the second line.
The ' indicates the transponate.
Exemple :
>> x = [1,2,3,4]
x =
1 2 3 4
>> y = [5;6;7;8]
y =
5
6
7
8
>> y'
ans =
5 6 7 8
>> A = [x;y']
A =
1 2 3 4
5 6 7 8
[x y] means horizontal cat of the vectors, while [x;y] means vertical.
For example (Horizontal cat):
x = [1
2
3];
y = [4
5
6];
[x y] = [1 4
2 5
3 6];
(Vertical cat):
x = [1 2 3];
y = [4 5 6];
[x; y] =
[1 2 3;
4 5 6];
Just to be clear, in MATLAB ' is the complex conjugate transpose. If you want the non-conjugating transpose, you should use .'.
It indicates the end of a row when creating a matrix from other matrices.
For example
X = [1 2];
Y = [3,4]';
A = [X; Y']
gives a matrix
A = [ 1 2 ]
[ 3 4 ]
This is called vertical concatenation which basically means forming a matrix in row by row fashion from other matrices (like the example above). And yes you are right about ' indicating the transpose operator. As another example you could use it to create a transposed vector as follows
Y = [1 2 3 4 5];
X = [1; 2; 3; 4; 5];
Y = Y';
Comparing the above you will see that X is now equal to Y. Hope this helps.
Let set the size of x m*n (m rows and n columns) and the size of y n*p.
Then A is the matrix formed by the vertical concatenation of x and the transpose of y (operator '), and its size is (m+p)*n. The horizontal concatenation is done with comma instead of semi-column.
This notation is a nice shorthand for function vertcat.
See http://www.mathworks.fr/help/techdoc/math/f1-84864.html for more information
The semicolon ' ; ' is used to start a new row.
e.g. x=[1 2 3; 4 5 6; 7 8 9] means
x= 1 2 3
4 5 6
7 8 9
So if u take x=[1 2 3; 4 5 6] and y=[7 8 9]'
then z=[x; y'] means
z= 1 2 3
4 5 6
7 8 9

Split array into pieces in MATLAB

I woutld like to split an array into equal pieces like this:
a=[1 2 3 4 5 6 7 8 9 10]
n = 2;
b = split(a, n);
b =
1 2 3 4 5
6 7 8 9 10
Which function can do this?
Try this:
a = [1 2 3 4 5 6]
reshape (a, 2, 3)
If a can be divided by n you can actually provide only one argument to RESHAPE.
To reshape to 2 rows:
b = reshape(a,2,[])
To reshape to 2 columns:
b = reshape(a,[],2)
Note that reshape works by columns, it fills the 1st column first, then 2nd, and so on. To get the desired output you have to reshape into 2 columns and then transpose the result.
b = reshape(a,[],2)'
You can place a check before reshape:
assert(mod(numel(a),n)==0,'a does not divide to n')