Re: check if a square matrix is diagonal

*To*: mathgroup at smc.vnet.net*Subject*: [mg115314] Re: check if a square matrix is diagonal*From*: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>*Date*: Fri, 7 Jan 2011 04:10:38 -0500 (EST)*References*: <ig3pkm$jh5$1@smc.vnet.net>

d is a list,so you can't use it as a loop bound. ConstantArray can use it as it is. Also: better use := instead of = in your definition checkIfDiagonalMatrix[m_] := Module[{d, mtemp}, d = Dimensions[m]; mtemp = DiagonalMatrix[Diagonal[m]] - m; If[mtemp == ConstantArray[0, d], True, False]] Cheers -- Sjoerd On Jan 6, 8:05 am, benyaya <xiao... at gmail.com> 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] > ]