how can I get together 2 tables containing different number of rows and colomns?
8 views (last 30 days)
Show older comments
I have 2 tables. One of them is 2160x7 table and the other one is 9444x9 table. Each colomn of the tables has a different name. How can I get them together?
3 Comments
Eric Sofen
on 18 Dec 2023
We're going to need more detail about the contents of the tables and how you want to combine them. What are the commonalities between the tables? But, as @Stephen23 says, the join functions are a good place to start.
Cris LaPierre
on 18 Dec 2023
I would recommend using the Join Tables Live Task to perform your join interactively. Once you find the settings that get you the result you want, you can convert the task to code.
Otherwise, please attach your data using the paperclip icon, as well as an example of what the output should be.
Answers (1)
Drishti
on 20 Sep 2024
Hi Okan,
For combining tables with different number of rows and columns you can refer to the MATLAB ‘join’ function.
The ‘join’ function is applicable if you have common columns or key variables. If the tables do not have any common column and differ in size as well, you can use ‘NaN’ values to facilitate the merging of tables.
Refer to the below code snippet for better understanding:
% Determine the maximum number of rows
maxRows = max(height(table1), height(table2));
% Pad the shorter table with NaN rows
if height(table1) < maxRows
% Extend table1 with NaN
table1{end+1:maxRows, :} = NaN;
end
if height(table2) < maxRows
% Extend table2 with NaN
table2{end+1:maxRows, :} = NaN;
end
% Horizontally concatenate the tables
combinedTable = [table1, table2];
Refer to the MATLAB Documentation of ‘join’ function to understand its functionality.
I hope this resolves your query.
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!