Re: check if a square matrix is diagonal

*To*: mathgroup at smc.vnet.net*Subject*: [mg115300] Re: check if a square matrix is diagonal*From*: "lumeng.dev at gmail.com" <lumeng.dev at gmail.com>*Date*: Fri, 7 Jan 2011 04:07:59 -0500 (EST)

d == Dimensions[m]; should be changed to d == Dimensions[m][[1]]; Also you need to check if m is square before computing a mtemp, for example, do SameQ @@ Dimensions[m] On Thu, Jan 6, 2011 at 01:06, benyaya <xiaochu 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] > ] > > -- Meng Lu (=E9=99=86=E8=90=8C) (+1) 614-312-4902 fax: +1-217-398-0747