Matrix formulation of electromagnetic scattering