Obtaining a matrix of complex values from associations giving the real and imaginary parts of each element?

Join[Values[matrix], 2].{1, I}

As J.M. pointed out, this can even be shortened to the following:

Values[matrix].{1, I}

A nice feature of this approach is that it produces packed arrays when possible.


Apply[Complex, matrix, {2}] (* or *)
Map[{#r, #i}.{1, I} &, matrix, {2}]

{{0.368252 +0.0199587 I,-0.461644+0.109868 I,-0.216081+0.562557 I,-0.479881-0.212978 I},
{0.105028 +0.632264 I,0.116589 -0.490063 I,0.463378 +0.231656 I,-0.148665+0.212065 I},
{0.463253 +0.201161 I,0.460547 +0.397829 I,0.222257 +0.0129121 I,0.168641 -0.544568 I},
{0.255221 -0.364687 I,0.191895 -0.337437 I,-0.12278+0.551195 I,0.560485 +0.134702 I}}


I'm a bit uncomfortable with both @HenrikSchumacher's and @kglr's (first) answers, as they rely on the correct sorting order of the keys in the Associations: they assume that every matrix element is precisely of the form of an association with first the "r" part and second the "i" part.

Without relying on the sorting order, or on the presence of both elements, and making more use of the essential features of associations, we can do

matrix /. a_Association :> Lookup[a, {"r", "i"}, 0].{1, I}

This solution works even if one matrix element is of the form <|"r" -> 2|> or <|"i" -> -3|> (it will default the other coordinate to zero). It also works if the order of the elements is interchanged: <|"r" -> 2, "i" -> -3|> gives the same result as <|"i" -> -3, "r" -> 2|>. It also works on higher-dimensional tensors or ragged structures.