Reputation: 17
I have two files with different rows but with the same number of columns. I want to combine these two data together, I could use vertcat but it will only combine the data below the first data, I want them to combine side by side.
file1 = readtable('table1.csv');
file2 = readtable('table2.csv');
Com = [file1; file2];
This works but I need the data, which I should get as if I am doing horizcat.
Thanks a lot for the help
Upvotes: 0
Views: 264
Reputation: 1362
You can only vertcat if both matrices contain the same number of columns, and you can only horzcat if they contain the same number of rows. Since your tables have a different number of rows you cannot horzcat them as is.
By padding the shorter matrix with extra values you can use horzcat, if that is acceptable. The code below will add a block of 0's to the bottom of the shorter matrix before combining the data side by side.
file1 = readtable('table1.csv');
file2 = readtable('table2.csv');
if (size(file1, 1) < size(file2,1))
file1 = [file1; zeros(size(file2,1)-size(file1,1), size(file1,2))];
else
file2 = [file2; zeros(size(file1,1)-size(file2,1), size(file2,2))];
end
Com = [file1 file2];
Upvotes: 1