Merge table with different rows
이전 댓글 표시
I have two files with different rows but with the same number of columns and I want to combine it together but I am getting an error * All tables in the bracketed expression must have the same number of rows. *
file1 = readtable('306K-268K.csv'); file2 = readtable('266K-228K.csv'); Com = [file1 file2];
Thanks a lot for the help
채택된 답변
추가 답변 (4개)
madhan ravi
2018년 11월 2일
편집: madhan ravi
2018년 11월 17일
2 개 추천
You can't merge table with different number of size dimensions , use structure instead
댓글 수: 8
Shelender Kumar
2018년 11월 18일
madhan ravi
2018년 11월 18일
편집: madhan ravi
2018년 11월 18일
A = [1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02
6.41E+01 7.83E-06 1.09E+02 ]
B = [1.38E-01 -1.02E-05 -8.81E-02
1.54E-01 NaN -5.51E-02 ;
zeros(1,size(A,2))] %PADDED WITH ZEROS because matrix dimensions should be the same when concatenated
side_by_side=[A B]
Shelender Kumar
2018년 11월 18일
madhan ravi
2018년 11월 18일
편집: madhan ravi
2018년 11월 18일
file1 = readtable('306K-268K.csv');
file2 = readtable('266K-228K.csv');
A=table2array(file1);
B=table2array(file2);
size(A)
size(B)
A=[A;zeros(size(B,1)-size(A,1),size(B,2))];
side_by_side = [A B]
size(side_by_side) %should be 681*80 if it is then we have achieved it
Shelender Kumar
2018년 11월 18일
madhan ravi
2018년 11월 18일
A=[A;zeros(abs(size(B,1)-size(A,1)),size(B,2))];
Shelender Kumar
2018년 11월 18일
madhan ravi
2018년 11월 18일
Glad to help :)
Stéphane Rainville
2018년 11월 16일
You're missing a semi-colon to invoke vertical concatenation ('vertcat') rather than default horizontal concatenation ('horzcat').
For instance, two tables with different number of rows (but same number of columns), this does NOT work:
myBigTable = [myTable1 myTable2];
But this does:
myBigTable = [myTable1; myTable2];
I was facing a similar problem when storing tables of different lengths in a cell array.
myTables{1} = myTable1;
myTables{2} = myTable2;
and using
bigTable = [myTables{:}]
did not work because unwrapping and concatenating cell contents invoked horizontal concatenation. You can't just stick a semi-colon in there, so I had to explicitly invoke vertical concatenation like this:
bigTable = vertcat(myTables{:});
댓글 수: 2
Shelender Kumar
2018년 11월 17일
편집: Shelender Kumar
2018년 11월 17일
Stéphane Rainville
2018년 11월 17일
Ah I see. Then yeah, tables of different length would be a problem.
Peter Perkins
2018년 11월 6일
"Merge" is kind of vague. It may be that you just need to add a semicolon to vertically concatenate:
Com = [file1; file2]
댓글 수: 3
Shelender Kumar
2018년 11월 17일
Peter Perkins
2021년 11월 8일
The current way to do this is to create the same number of rows in the smaller matrix/table/whatever. That's already been shown in an earlier reply.
카테고리
도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!