why is there a horzcat error with this x = [eye(3),2*ones(2,3);3*ones(2,5)] in matlab but dont use repmat in matlab

The horzcat error is occurring because the dimensions of the matrices you are trying to concatenate horizontally don't match.

To solve this error without using repmat, you can use the padarray function in MATLAB to resize the matrices before concatenating them. Here's how you can modify your code:

main.m
x = [eye(3), padarray(2*ones(2,3), [0 2], 'post'); padarray(3*ones(2,5), [0 0], 'post')]
89 chars
2 lines

In this code, padarray is used to append additional rows or columns of zeros to the matrices. The [0 2] parameter in the first padarray call adds 2 columns of zeros to the second matrix, while the [0 0] parameter in the second padarray call doesn't add any additional columns.

This modified code will create a matrix x where the dimensions of the concatenated matrices match, avoiding the horzcat error.

related categories

gistlibby LogSnag