ellogico
ellogico

Reputation: 11

Horizontally concatenate column vectors

I have two vectors in MATLAB:

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

I want to create a vector c as shown below, but I can't figure out how to do it.

c = [1 2 3 4 5 6 7]

Upvotes: 0

Views: 319

Answers (2)

Robert Seifert
Robert Seifert

Reputation: 25232

If you are not sure whether your input vectors are vertical or not, use this robust way:

c = [ a(:); b(:) ];

The colon will make a column vector out of every vector, row AND column vectors. So you can concatenate them by [ ... ; ... ]. If you want a row vector at the end, you need to transpose the final result:

c = [ a(:); b(:) ].';

Upvotes: 2

omerfar23
omerfar23

Reputation: 34

Simple, just use c = [ a b ] should give you c = [1 2 3 4 5 6 7]

Upvotes: 0

Related Questions