Re: Transforming matrices

• To: mathgroup at smc.vnet.net
• Subject: [mg29672] Re: Transforming matrices
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Tue, 3 Jul 2001 04:40:25 -0400 (EDT)
• References: <9hp4pa\$2ef\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

Michael,

Here are two ways - the second seems to be the quicker one:

rates= {40, 38,37,36,33,32,31,30,27,23,22,21,20,16,14,13,12,10,9,6};

mat={{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}};

FIRST WAY

f[0]:= (++i;rates[[k]])
f[_]:=(k=++i;0)

(i=0;k=1;f/@#)&/@ mat

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

SECOND WAY

g[0,_]:=rates[[k]];
g[_,{n_}]:=(k=n;0)

(k=1;MapIndexed[g,#])&/@mat

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

"Michael Loop" <loopm at yahoo.com> wrote in message
news:9hp4pa\$2ef\$1 at smc.vnet.net...
> 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

• Prev by Date: Re: Transforming matrices
• Next by Date: RE: Transforming matrices
• Previous by thread: Re: Transforming matrices
• Next by thread: RE: Transforming matrices