Skip to main content
2 of 4
added 97 characters in body
Carl Lange
  • 13.2k
  • 1
  • 38
  • 70

How to join two matrix with different dimensions

I have two matrices with different dimensions I want to join them together in one matrix.

list1 = {{"Dis", "S1", "S2", "S3", "S4", "S5", "S6"},
         {"D1", 1, 1, 0, 1, 1, 0}, {"D2", 1, 0, 1, 0, 0, 1}, {"D3", 0, 0, 1, 1, 0, 1},
         {"D4", 0, 1, 0, 0, 1, 1}, {"D5", 1, 1, 1, 0, 1, 1}, {"D6", 1, 1, 0, 0, 0, 1},
         {"D7", 0, 1, 1, 1, 0, 0}};
List2 = {{"Dis", "S7", "S8", "S4", "S9", "S10", "S3", "S11", "S12", "S5", "S1"},
         {"D2", 0, 1, 0, 0, 1, 0, 1, 1, 0, 0}, {"D8", 1, 1, 1, 1, 1, 0, 0, 0, 1, 0},
         {"D9", 1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, {"D10", 0, 0, 1, 0, 0, 1, 1, 0, 0, 0},
         {"D6", 0, 1, 0, 0, 1, 0, 1, 0, 0, 1}, {"D11", 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
         {"D12", 0, 1, 0, 1, 0, 1, 0, 0, 0, 0}, {"D13", 1, 0, 1, 0, 1, 1, 0, 0, 0, 1},
         {"D14", 0, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {"D15", 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
         {"D16", 0, 0, 0, 1, 0, 0, 0, 0, 1, 1}, {"D17", 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}};

in order to get output like that:

{{"Dis", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", 
  "S11", "S12"}, {"D1", 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {"D2", 1,
   0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1}, {"D3", 0, 0, 1, 1, 0, 1, 0, 0, 0,
   0, 0, 0}, {"D4", 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D5", 1, 1, 
  1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D6", 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 
  0, 1}, {"D7", 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {"D8", 1, 1, 1, 
  1, 1, 0, 1, 0, 1, 1, 0, 0}, {"D9", 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 
  1}, {"D10", 0, 0, 1, 0, 0, 1, 1, 0, 0, 0}, {"D6", 0, 1, 0, 0, 1, 0, 
  1, 0, 0, 1}, {"D11", 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1}, {"D12", 0,
   0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0}, {"D13", 1, 0, 1, 4, 0, 0, 1, 0, 
  0, 1, 0, 0}, {"D14", 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1}, {"D15", 0,
   0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0}, {"D16", 0, 0, 0, 0, 1, 0, 0, 0, 
  1, 0, 0, 0}, {"D17", 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1}}