Re: Transforming matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg29688] Re: Transforming matrices
- From: "Orestis Vantzos" <atelesforos at hotmail.com>
- Date: Tue, 3 Jul 2001 04:40:43 -0400 (EDT)
- Organization: National Technical University of Athens, Greece
- References: <9hp4pa$2ef$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
As far as I can see, you don't really transform the matrix as a whole, but every row on its own as a vector. So here is a function that does what you asked: toRates[v_, r_] := Module[{p = Transpose[{Take[r, Length[v]], v /. 2 -> 1}]}, p = Split[p, #1[[2]] == #2[[2]] &]; p = Replace[ p, {L0 : {{_, 0} ..} :> 0[L0[[1, 1]], Length[L0]], L1 : {{_, 1} ..} :> 1[L1[[-1, 1]], Length[L1]]}, 1]; p = p //. {L___, 1[n_, l1_], 0[m_, l2_], R___} /; n != m -> {L, 1[n, l1], 0[n, l2], R}; Flatten[ Replace[p, {0[n_, l_] :> Table[n, {l}], 1[n_, l_] :> Table[0, {l}]}, 1]]] If M is your matrix and r is the vector of rates do this: toRates[#,r]& /@ M Orestis PS.This probably will take a lot of time, no matter what you do. I hope you don't do this transformation in a loop ;-) "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