Re: Adding a value to the cell of a matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg76062] Re: Adding a value to the cell of a matrix
• From: Ray Koopman <koopman at sfu.ca>
• Date: Tue, 15 May 2007 05:05:29 -0400 (EDT)
• References: <f23qem\$o48\$1@smc.vnet.net>

```On May 12, 12:30 am, newbie <purete... 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
>
> 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}];
>
> gcounter = scounter = gmcounter = mgcounter = sgcounter = 0;
>
> 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}]

This will give five 18-element lists of counts.
Although "l" is {5164, 18}, you use only the first 200 elements
in each column, so those are the only ones I put into "L".
It's much easier to work with rows than columns.

L = Ordering[#,200]& /@ Transpose @ matrix1; (*{5164,18}->{18,200}*)

orderedcellcycle = annotations[[#,6]]& /@ L;   (* {18,200} *)

gcount  = Count[#,"G1"  ]& /@ orderedcellcycle;   (* {18} *)
scount  = Count[#,"S"   ]& /@ orderedcellcycle;   (* {18} *)
sgcount = Count[#,"S/G2"]& /@ orderedcellcycle;   (* {18} *)
gmcount = Count[#,"G2/M"]& /@ orderedcellcycle;   (* {18} *)
mgcount = Count[#,"M/G1"]& /@ orderedcellcycle;   (* {18} *)

If you have many patterns for which you need counts, you might want
to do something like this, which gives a {5, 18} matrix of counts:

patternlist = {"G1", "S", "S/G2", "G2/M", "M/G1"};

countmatrix = Outer[Count[#2,#1]&, patternlist, orderedcellcycle, 1];

```

• Prev by Date: Re: Re: Package Help in Mathematica 6
• Next by Date: Re: Compatibility woes
• Previous by thread: Re: Adding a value to the cell of a matrix