An incidence matrix of all subsets
It isn't quite clear to me what is wanted, but the matrix in the OP can be generated readily, with a little help from an undocumented function:
With[{n = 3},
SparseArray`SparseBlockMatrix[MapIndexed[Join[#2, #2] -> {#1} &,
Unitize[Subsets[Range[n],
{1, ∞}]]]]] // MatrixForm
$$\begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ \end{pmatrix}$$
sL = Rest[Subsets@Range@3]
{{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
You can also use SparseArray
+ Band
:
ClearAll[f]
f = SparseArray[Band[{1, 1}] -> List /@ Unitize@#] &;
f @ sL // MatrixForm // TeXForm
$\left( \begin{array}{cccccccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ \end{array} \right)$
Alternatively, a combination of MapThread
+ RotateRight
+ PadLeft
+ Accumulate
:
ClearAll[g]
g = Module[{cl = Accumulate[Length /@ #]},
MapThread[RotateRight, {PadLeft[Unitize @ #, {Automatic, Last @ cl}], cl}]] &
g @ sL // MatrixForm // TeXForm
same result
If you wish to use an integer as input, you can modify f
as follows:
ClearAll[f2]
f2 = SparseArray[Band[{1, 1}] -> List /@ Unitize @ Rest @ Subsets @ Range @ #] &;
f2 @ 3 // MatrixForm // TeXForm
$\left( \begin{array}{cccccccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ \end{array} \right)$
You can modify g
similarly.
Clear["Global`*"]
n = 3;
L = Range[n];
SL = Subsets[L, {1, n}]
(* {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}} *)
Freestanding commas do not make much sense. Perhaps you mean:
(mat = Module[{len = Length@SL},
Array[ConstantArray[KroneckerDelta@##, Length[SL[[#2]]]] &, {len,
len}]]) // MatrixForm