1
$\begingroup$

Given some lists

lstRow1 = {{"C1", "C2", "C3"}, {1, 2, 3}};
lstRow2 = {{"C2", "D1", "D2"}, {4, 5, 6}};
lstRow3 = {{"C1", "D2", "D3"}, {7, 8, 9}};
lstRow4 = {{}, {}};
lstRow5 = {{"D1", "D2", "E1"}, {10, 11, 12}};
lstRow6 = {{}, {}};

lstHead = {{"C1", "H1"}, {"C2", "H1"}, {"C3", "H1"}, {"D1", 
    "H2"}, {"D2", "H2"}, {"D3", "H2"}, {"E1", "H3"}};

how to build a sparse arrary like this:

lstSparse = {
   {"H1", "0", "0", "H2", "0", "0", "H3"},
   {"C1", "C2", "C3", "D1", "D2", "D3", "E1"}, {1, 2, 3, 0, 0, 0, 
    0}, {0, 4, 0, 5, 6, 0, 0}, {7, 0, 0, 0, 8, 9, 0}, {0, 0, 0, 0, 0, 
    0, 0}, {0, 0, 0, 10, 11, 0, 12}, {0, 0, 0, 0, 0, 0, 0}};

or ZEROs could be replaced by blank string, to be like this. $$\left( \begin{array}{ccccccc} \text{H1} & \text{0} & \text{0} & \text{H2} & \text{0} & \text{0} & \text{H3} \\ \text{C1} & \text{C2} & \text{C3} & \text{D1} & \text{D2} & \text{D3} & \text{E1} \\ 1 & 2 & 3 & 0 & 0 & 0 & 0 \\ 0 & 4 & 0 & 5 & 6 & 0 & 0 \\ 7 & 0 & 0 & 0 & 8 & 9 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 10 & 11 & 0 & 12 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right) \to \left( \begin{array}{ccccccc} \text{H1} & \text{} & \text{} & \text{H2} & \text{} & \text{} & \text{H3} \\ \text{C1} & \text{C2} & \text{C3} & \text{D1} & \text{D2} & \text{D3} & \text{E1} \\ 1 & 2 & 3 & \text{} & \text{} & \text{} & \text{} \\ \text{} & 4 & \text{} & 5 & 6 & \text{} & \text{} \\ 7 & \text{} & \text{} & \text{} & 8 & 9 & \text{} \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} \\ \text{} & \text{} & \text{} & 10 & 11 & \text{} & 12 \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} \\ \end{array} \right)$$

$\endgroup$
1
$\begingroup$
rows = {lstRow1, lstRow2, lstRow3, lstRow4, lstRow5, lstRow6};
ac = ArrayComponents[rows[[All, 1]]];
sa = SparseArray[Join @@ (Thread /@ Thread[MapIndexed[{#2[[1]], #} &, ac, {2}] -> 
        rows[[All, 2]]]), {Length@rows, Max@ac}];

sa // MatrixForm // TeXForm

$\left( \begin{array}{ccccccc} 1 & 2 & 3 & 0 & 0 & 0 & 0 \\ 0 & 4 & 0 & 5 & 6 & 0 & 0 \\ 7 & 0 & 0 & 0 & 8 & 9 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 10 & 11 & 0 & 12 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Alternatively, use "" as the background element

sa2 = SparseArray[Join @@ (Thread /@ Thread[MapIndexed[{#2[[1]], #} &, ac, {2}] -> 
            rows[[All, 2]]]), {Length@rows, Max@ac}, ""];
sa2 // MatrixForm // TeXForm

$\left( \begin{array}{ccccccc} 1 & 2 & 3 & \text{} & \text{} & \text{} & \text{} \\ \text{} & 4 & \text{} & 5 & 6 & \text{} & \text{} \\ 7 & \text{} & \text{} & \text{} & 8 & 9 & \text{} \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} \\ \text{} & \text{} & \text{} & 10 & 11 & \text{} & 12 \\ \text{} & \text{} & \text{} & \text{} & \text{} & \text{} & \text{} \\ \end{array} \right)$

headers = Join @@@ Transpose[PadRight /@ MapIndexed[{{"H" <> ToString[#2[[1]]]}, #} &, 
     GatherBy[DeleteDuplicates[Join @@ rows[[All, 1]]], StringTake[#, 1] &]]]

{{0, "H1", 0, 0, "H2", 0, 0, "H3"}, {0, "C1", "C2", "C3", "D1", "D2", "D3", "E1"}}

Join[headers, sa] // TeXForm

$\left( \begin{array}{ccccccc} \text{H1} & 0 & 0 & \text{H2} & 0 & 0 & \text{H3} \\ \text{C1} & \text{C2} & \text{C3} & \text{D1} & \text{D2} & \text{D3} & \text{E1} \\ 1 & 2 & 3 & 0 & 0 & 0 & 0 \\ 0 & 4 & 0 & 5 & 6 & 0 & 0 \\ 7 & 0 & 0 & 0 & 8 & 9 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 10 & 11 & 0 & 12 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$

Update: Obtaining header rows from lstHead:

headers2 = {SequenceReplace[#[[1]], {b : (a_String) ..} :> 
     Sequence[a, ## & @@ ConstantArray[0, Length[{b}] - 1]]], #[[2]]} &@
  Reverse[Transpose[lstHead]]

{{"H1", 0, 0, "H2", 0, 0, "H3"}, {"C1", "C2", "C3", "D1", "D2", "D3", "E1"}}

$\endgroup$
  • $\begingroup$ Thanks! could the values H1`H2`H3 in the head be picked from the original values in lstHead other than generated by codes? $\endgroup$ – Jerry Nov 28 '18 at 2:55
  • $\begingroup$ @Jerry, thank you for the accept. I will post an update re pulling the first row headers from input data. $\endgroup$ – kglr Nov 28 '18 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.