0
$\begingroup$

How can I merge indices, such as converting a $4$-index list to $2$-index matrix, $C_{ijkl} \rightarrow D_{(ik); (jl)}$? This example sometimes appears in physics applications.

Here, I mean $(ik)$ as a multi-index: if $i$ runs from $1$ to $m$ and $k$ runs from $1$ to $n$, then $(ik)$ runs over $n*m$ values from $1,1$ to $n,m$. That is, $(i,k)$ is ordered going like $(1,1), (1,2)...(1,m), (2,1),(2,2)...(2,m),...(n,m)$


To clarify what I mean, here is an example input and output.

[In]: a = Table[i*j*(k + 1)*l, {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}];
FourIndtoTwoInd[a]

[Out]: {{2,4,4,8}, {3,6,6,12}, {4,8,8,16}, {6,12,12,24}}

For example, the first row has values ${2,4,4,8}$, corresponding to $i,k=(1,1)$ and $j,l = (1,1), (1,2), (2,1), (2,2)$.

$\endgroup$

1 Answer 1

3
$\begingroup$

You may use Flatten level combining syntax.

With a as in OP then

Flatten[a, {{2, 3}, {1, 4}}]
{{2, 4, 4, 8}, {3, 6, 6, 12}, {4, 8, 8, 16}, {6, 12, 12, 24}}

Which combines levels 2 and 3 in the order of levels 1 and 4.

Hope this helps

$\endgroup$

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.