MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: check if a square matrix is diagonal

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115332] Re: check if a square matrix is diagonal
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Fri, 7 Jan 2011 04:14:09 -0500 (EST)

On Jan 6, 2011, at 2:06 AM, 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

 If[mtemp == Table[Table[0, {i, 1, d[[1]]}], {i, 1, d[[2]]}],
  True,
  False]

Better yet, why not just check that all the off diagonal entries are 0?

checkIfDiagonalMatrix[m_] == Block[{},Do[If[m[[i,j]]!==0,Return[False]],{i,Length[m]},{j,Length[m[[1]]]}];True]

Regards,
	Ssezi


  • Prev by Date: Re: Using Extract where some indices are out of bounds (efficiently)
  • Next by Date: Re: How to change the directory for the docs?
  • Previous by thread: Re: check if a square matrix is diagonal
  • Next by thread: Re: check if a square matrix is diagonal