1 Row and Column Combine Stack Tables and Matrices

Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).

1.1 Generate Some Tables and Matrixes for Combination

close all;

% Generate Table 1
ar_fl_abc1 = [0.4 0.1 0.25 0.3 0.4];
ar_fl_abc2 = [0.4 0.1 0.2 0.3 0.4];
number1 = '123';
number2 = '456';
mt_data_a = [ar_fl_abc1' ar_fl_abc2'];

tb_test_a = array2table(mt_data_a);
cl_col_names_a = {['col' num2str(number1)], ['col' num2str(number2)]};
cl_row_names_a = strcat('rowA=', string((1:size(mt_data_a,1))));

tb_test_a.Properties.VariableNames = cl_col_names_a;
tb_test_a.Properties.RowNames = cl_row_names_a;
disp(tb_test_a);

              col123    col456
              ______    ______

    rowA=1      0.4      0.4  
    rowA=2      0.1      0.1  
    rowA=3     0.25      0.2  
    rowA=4      0.3      0.3  
    rowA=5      0.4      0.4  

% Generate Table 2
rng(123);
ar_fl_abc3 = rand(size(ar_fl_abc1));
ar_fl_abc4 = rand(size(ar_fl_abc1));
ar_fl_abc5 = rand(size(ar_fl_abc1));

mt_data_b = [ar_fl_abc3' ar_fl_abc4' ar_fl_abc5'];

tb_test_b = array2table(mt_data_b);
cl_col_names_b = {['col' num2str(33)], ['col' num2str(44)], ['col' num2str(55)]};
cl_row_names_b = strcat('rowB=', string((1:size(mt_data_a,1))));

tb_test_b.Properties.VariableNames = cl_col_names_b;
tb_test_b.Properties.RowNames = cl_row_names_b;
disp(tb_test_b);

               col33      col44      col55  
              _______    _______    ________

    rowB=1    0.69647    0.42311     0.34318
    rowB=2    0.28614    0.98076     0.72905
    rowB=3    0.22685    0.68483     0.43857
    rowB=4    0.55131    0.48093    0.059678
    rowB=5    0.71947    0.39212     0.39804

1.2 Combine Tables Together Stack Columns

Tables with the same number of rows, add more columns with named variables

% a and b must have the same row names
tb_test_b_withArownames = tb_test_b;
tb_test_b_withArownames.Properties.RowNames = tb_test_a.Properties.RowNames;
tb_ab_col_stacked = [tb_test_a tb_test_b_withArownames];
disp(tb_ab_col_stacked);

              col123    col456     col33      col44      col55  
              ______    ______    _______    _______    ________

    rowA=1      0.4      0.4      0.69647    0.42311     0.34318
    rowA=2      0.1      0.1      0.28614    0.98076     0.72905
    rowA=3     0.25      0.2      0.22685    0.68483     0.43857
    rowA=4      0.3      0.3      0.55131    0.48093    0.059678
    rowA=5      0.4      0.4      0.71947    0.39212     0.39804

1.3 Combine Tables Together Stack Rows

Tables with the same number of columns, dd more rows variables

% Select only 2 columns to match table a column count
tb_test_b_subset = tb_test_b(:,1:2);

% Make Column Names consistent
tb_test_b_subset.Properties.VariableNames = cl_col_names_a;

% Reset Row Names, can not have identical row names
tb_test_a.Properties.RowNames = strcat('row=', string((1:size(mt_data_a,1))));
tb_test_b_subset.Properties.RowNames = ...
    strcat('row=', string(((size(mt_data_a,1)+1):(size(mt_data_a,1)+size(tb_test_b_subset,1)))));
% tb_test_b_subset.Properties.RowNames =

% Stack Rows
tb_ab_row_stacked = [tb_test_a; tb_test_b_subset];
disp(tb_ab_row_stacked);

              col123     col456 
              _______    _______

    row=1         0.4        0.4
    row=2         0.1        0.1
    row=3        0.25        0.2
    row=4         0.3        0.3
    row=5         0.4        0.4
    row=6     0.69647    0.42311
    row=7     0.28614    0.98076
    row=8     0.22685    0.68483
    row=9     0.55131    0.48093
    row=10    0.71947    0.39212