MathGroup Archive 2001

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

Search the Archive

RE: Transforming matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29671] RE: [mg29665] Transforming matrices
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Tue, 3 Jul 2001 04:40:25 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: loopm at yahoo.com [mailto:loopm at yahoo.com]
To: mathgroup at smc.vnet.net
> Sent: Monday, July 02, 2001 8:20 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg29671] [mg29665] Transforming matrices
> 
> 
> I am a relatively new user of Mathematica, and I am having some
> trouble transforming a matrix.  I would be appreciative of any advice.
> The problem is as follows.
> 
> Given a randomly formed matrix of form:
>  
> 0 0 1 2 2 2 0 0 0 0 0 0 1 2 0 1 2 2 2 0
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 1 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>  
> Each digit represents a different state, and therefore each digits
> position must be preserved.  I need to convert this matrix into a
> matrix of rates instead.  The zeros will be converted into constant
> rates dependent on their position, and the rate will continue until a
> 1 is reached.  Both 1 and 2 represent a rate of 0.  So given a vector
> of rates:
>  
> 40 38, 37, 36, 33, 32, 31, 30, 27, 23, 22, 21, 20, 16, 14, 13, 12, 10,
> 9, 6
>  
> I need to convert the first randomly generated matrix to look like:
>  
> 40 40  0  0  0  0 32 32 32 32 32 32  0  0 16  0  0  0  0  9
> 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40
> 40 40  0  0  0  0 32 32 32 32 32 32 32 32 32 32 32 32 32 32
>  
> Besides the fact that the rate must stay constant until a 1 is
> reached, the other subtlety of my problem is that when a rate starts
> over after a 1 or 2, the new rate must begin in the position of the
> next zero, but the rate must come from the position of the last 1 or
> 2.  I can convert a matrix of this form on an individual basis using
> the Position and Table commands, but cannot find an efficient way of
> converting the matrix on a large scale.  The matrix I actually need to
> convert is 240*1000, so if anyone can think of an efficient way of
> doing this I would be grateful for your input.  Thank you.
>  
> Micahel Loop
> Minneapolis, MN
> 

Michael,

calling your "matrix" of states sss, and the "vector" of rates rr, we 
define a small helper, which sets and reminds the current rate and 
outputs what is needed:

In[34]:= f[0, _] := srate;
         f[_, rate_] := (srate = rate; 0)

ff now initializes the current rate and maps over states and rates:

In[36]:= ff[states_List, rates_List] := 
        (f[2, First[rates]];    (* initializes current rate *)
         MapThread[f, {states, rates}])

To do all calculation

In[37]:= Thread[head[sss, rr], List, 1] /. head -> ff
Out[37]=
{{40,40, 0, 0, 0, 0,32,32,32,32,32,32, 0, 0,16, 0, 0, 0, 0, 9},
 {40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40},
 {40,40, 0, 0, 0, 0,32,32,32,32,32,32,32,32,32,32,32,32,32,32}}

Don't be confounded by this expression, the head has to be 
temoraryly held to reach for correct threading before evaluation 
of ff, however this more simple expression does the same:

In[38]:= ff[#, rr] & /@ sss

>From your examples I could not recognize the difference between 
states 1 and 2. Adapt that as needed when defining f.

-- Hartmut 



  • Prev by Date: Re: Transforming matrices
  • Next by Date: Re: Transforming matrices
  • Previous by thread: Re: Transforming matrices
  • Next by thread: Re: Problems with TeX and EPS output (v3.0, mac)