why is there a horzcat error with this x = [eye(3),2*ones(2,3);3*ones(2,5)] in matlab with the simpliest fix in matlab

The error "Error using horzcat CAT arguments dimensions are not consistent." occurs because the number of rows in each matrix being horizontally concatenated (horzcat) are not the same.

In the example you provided, you are trying to horizontally concatenate [eye(3),2ones(2,3)] with [3ones(2,5)]. The first matrix has 3 rows while the second matrix has 2 rows.

To fix this error, you can either adjust the dimensions of the matrices so that they have the same number of rows, or you can vertically concatenate (vertcat) the matrices instead of horizontally concatenating them.

If you want to vertically concatenate the matrices, you can use the following code:

main.m
x = [eye(3); 2*ones(2,3); 3*ones(2,5)];
40 chars
2 lines

This will result in x being a matrix with 7 rows and 5 columns.

Alternatively, if you want to horizontally concatenate the matrices, you can adjust the dimensions of the second matrix to have 3 rows instead of 2. Here's an example:

main.m
x = [eye(3), 2*ones(3,3); 3*ones(3,5)];
40 chars
2 lines

This will result in x being a matrix with 3 rows and 8 columns.

related categories

gistlibby LogSnag