horzcat two different sizes in matlab

In MATLAB, to horizontally concatenate two arrays with different sizes, you need to make sure that one dimension of both arrays is same. For example, if you have an array of size [3 2] and another array of size [3 1], then you need to make sure that the first dimension is same, i.e., 3 in this case.

Here's an example:

main.m
a = [1 2; 3 4; 5 6];
b = [7; 8; 9];
c = horzcat(a, b) % concatenate a and b horizontally

% Output:
% c =
% 
%      1     2     7
%      3     4     8
%      5     6     9
172 chars
11 lines

In the above example, a is a 3x2 matrix and b is a 3x1 matrix. We can horizontally concatenate a and b by using the horzcat function. The output c is a 3x3 matrix, where the first two columns are from a and the last column is from b.

related categories

gistlibby LogSnag