Re: check if a square matrix is diagonal
- To: mathgroup at smc.vnet.net
- Subject: [mg115330] Re: check if a square matrix is diagonal
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Fri, 7 Jan 2011 04:13:47 -0500 (EST)
- References: <ig3pkm$jh5$1@smc.vnet.net>
benyaya wrote:
> What I try to do is extract the diagonal, subtract if from the matrix,
> then compare the new matrix with 0.
> My code doesn't work out though, can anyone help? thanks a lot.
>
> checkIfDiagonalMatrix[m_] = Module[{d, mtemp},
> d = Dimensions[m];
> mtemp = DiagonalMatrix[Diagonal[m]] - m;
> If[mtemp == Table[Table[0, {i, 1, d}], {i, 1, d}],
> True,
> False]
> ]
>
Try this:
isDiagonalMatrix[m_?MatrixQ] := (DiagonalMatrix@Diagonal[m] == m)
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de