       Re: Adding a value to the cell of a matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg75903] Re: [mg75880] Adding a value to the cell of a matrix
• From: DrMajorBob <drmajorbob at bigfoot.com>
• Date: Sun, 13 May 2007 05:42:16 -0400 (EDT)
• References: <2868213.1178958082081.JavaMail.root@m35>

```Never, EVER use do loops. (OK, sometimes. As a last resort.)

any = Table[RandomReal[], {i, 5}, {j, 4}];
gt0 = Map[If[# > 1/2, 1, 0] &, any, {2}]

{{0, 0, 1, 1}, {1, 1, 1, 1}, {0, 0, 1, 0}, {1, 0, 1, 0}, {1, 0, 0, 1}}

(1 indicates a number > 1/2, 0 otherwise.)

This counts occurrences by column:

Plus @@ gt0

{3, 1, 4, 3}

or

Total /@ Transpose[gt0]

{3, 1, 4, 3}

Counting by row:

Total /@ gt0

{2, 4, 1, 2, 2}

Bobby

On Sat, 12 May 2007 02:15:03 -0500, newbie <puretexan at gmail.com> wrote:

> I have a matrix of values that i am searching for a specific value in
> and i want to count the number of occurances in each column. which i
> can do but not in a loop.  I want to be able to use a matrix to store
> the number of occurances so i can put it in a loop. I already have a
> loop that finds the values and just adds them to a value i created. do
> you know how i can add +1 to the cell of a matrix? When i try to do
> this it says the values are protected. i tried unprotecting them but
> couldn't get that to work.
>
>
>  any ideas?
>
> Here is the code i am using for the search
>
>
> In:=Orderedcellcycle=OverExpressed; (* Dimensions {200, 18}*)
> l=matrix1;   (*{5164, 18}*)
> Do[l[[All,i]]=Ordering[matrix1[[All,i]]],{i,1,18}];
> Do[Do[Orderedcellcycle[[i,j]]=annotations[[l[[i,j]],6]],{i,1,200}],{j,
> 1,18}];
>
> In:=gcounter=scounter=gmcounter=mgcounter=sgcounter=0;
>
> In:=Do[Do[If[Orderedcellcycle[[i,j]]=="G1",gcounter=gcounter
> +1,gcounter=\
> gcounter+0],{i,1,200}];
>   Do[If[Orderedcellcycle[[i,j]]=="S",scounter=scounter+1,scounter= >           scounter+0],{i,1,200}];
>   Do[If[Orderedcellcycle[[i,j]]=="S/G2",sgcounter=sgcounter
> +1,sgcounter=
>         sgcounter+0],{i,1,200}];
>   Do[If[Orderedcellcycle[[i,j]]=="G2/M",
>         gmcounter=gmcounter+1,gmcounter=gmcounter+0],{i,1,200}];
>   Do[If[Orderedcellcycle[[i,j]]=="M/G1",mgcounter=mgcounter
> +1,mgcounter=
>       mgcounter+0],{i,1,200}],{j,1,18}]
>
>
>

--

DrMajorBob at bigfoot.com

```

• Prev by Date: Re: Re: question 1
• Next by Date: Re: 6.0 looks really nice so far.
• Previous by thread: Adding a value to the cell of a matrix
• Next by thread: Re: Adding a value to the cell of a matrix