MathGroup Archive 2001

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

Search the Archive

Re: checking for a particular value in a matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30954] Re: [mg30933] checking for a particular value in a matrix
  • From: BobHanlon at aol.com
  • Date: Fri, 28 Sep 2001 03:55:02 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 2001/9/27 2:32:57 AM, auhb3 at cz3.nus.edu.sg writes:

>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]}]
>

onlyOneOne[a_?VectorQ] := 
    Count[a,0]==(Length[a]-1) && 
      Count[a,1]==1;

onlyOneOne[{0,0,0,1}]

True

onlyOneOne /@ {{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,2}}

{True, True, True, False}

convert[a_]:=Sum[a[[i]]*2^(Length[a]-i),{i,1,Length[a]}];

a = Table[Random[Integer], {8}];

convert[a] == FromDigits[a, 2]

True

a = {a1,a2,a3,a4};

convert[a] == FromDigits[a, 2] // ExpandAll

True


Bob Hanlon
Chantilly, VA  USA


  • Prev by Date: RE: integer to string??
  • Next by Date: Stuck in a For Loop
  • Previous by thread: Re: checking for a particular value in a matrix
  • Next by thread: Re: checking for a particular value in a matrix