MathGroup Archive 2001

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

Search the Archive

RE: checking for a particular value in a matrix

  • To: mathgroup at
  • Subject: [mg30961] RE: [mg30933] checking for a particular value in a matrix
  • From: "David Park" <djmp at>
  • Date: Fri, 28 Sep 2001 03:55:11 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

This code will check that there is exactly one entry in a matrix of value 1.

ExactlyOneOne[(mat_)?MatrixQ] :=
  Count[mat, 1, Infinity] == 1

testmat1 = {{1, 0}, {2, 3}};
testmat2 = {{1, 0}, {2, 1}};
testmat3 = {{-1, 0}, {2, 3}};

ExactlyOneOne /@ {testmat1, testmat2, testmat3}
{True, False, False}

As for your second question, use a DelayedSet (:=). When you use Set (=)
Mathematica calculates the right hand side of the definition immediately -
even though the symbol a appears on the left hand side. This is a confusing
aspect of Mathematica definitions that trips up many users. I don't know why
Mathematica can't check to see if the symbol appears on the lhs. There is
probably some logic to it.

David Park
djmp at

> From: Au Han Bin [mailto:auhb3 at]
To: mathgroup at
> Hi, I am a programming newbie and I need advice on how to check that a
> table or matrix , for all nonzero entries in a given row, has only one
> entry of value 1.
> i have another query on why the code below does not work, when i call
> convert{1,0,0,0} again, the results given are from an earlier question,
> e.g. {1,1} is it initialised wrongly?
> Clear[a,convert];
> convert[a___] = Sum[a[[i]]*2^(Length[a]-i),{i,1,Length[a]}]
> regards, thanks,
> joshua

  • Prev by Date: RE: Re: ReadList, mix Words & Numbers?
  • Next by Date: Mathematica 4.0 installation problem
  • Previous by thread: Re: checking for a particular value in a matrix
  • Next by thread: RE: checking for a particular value in a matrix