Re: checking for a particular value in a matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg30950] Re: checking for a particular value in a matrix
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Fri, 28 Sep 2001 03:54:58 -0400 (EDT)
• References: <9ouhbm\$198\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Joshua

1) oneOneQ2[mat_,r_]:= Cases[mat[[r]],1,{1},2]== {1}

or

oneOneQ2s[mat_,r_]:= Cases[mat[[r]],1]== {1}

The first one is likely to be quicker if you have many ones in the rows.

2)

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

I am assuming that you want a to be a list.
The other change is to  use SetDelayed (:=) instead of Set(=). This
preventst the right side of the assignment from evaluaing (to zero) before
the rule is stored.

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Au Han Bin" <auhb3 at cz3.nus.edu.sg> wrote in message
news:9ouhbm\$198\$1 at smc.vnet.net...
> 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: FileName and Shell Command
• Next by Date: Strange syntax errors when writing functions ?
• Previous by thread: Re: checking for a particular value in a matrix
• Next by thread: RE: checking for a particular value in a matrix