Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: Transforming matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29685] Re: [mg29665] Transforming matrices
  • From: Tomas Garza <tgarza01 at prodigy.net.mx>
  • Date: Tue, 3 Jul 2001 04:40:40 -0400 (EDT)
  • References: <200107020620.CAA02313@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi, Michael!
I thought I'd try to give an answer to your problem. The idea goes as
follows: given your matrix, say, mat, take a new matrix, newMat, where you
substitute all 1's and 2's by the symbol "*". Then (in the first row, for
example), look for all the positions in that row where you have a sequence
*0. The sequence of such positions determine the positions of the
consecutive rates you are going to substitute the zeros with. Then, apply
this substitution to all rows, and you're done. I propose the following
code, where I include the case where some rows could start with a 1 instead
of a 0. Take the matrix 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}, {1, 2, 2, 0, 0, 0, 1, 2, 0, 1, 2, 2,
2,
     0, 0, 0, 1, 2, 2, 0}}

 (the same you have in your query, with an extra row starting with 1). Then,

In[2]:=
newMat = mat /. {1 -> "*", 2 -> "*"}
Out[2]=
{{0, 0, "*", "*", "*", "*", 0, 0, 0, 0, 0, 0, "*", "*", 0, "*", "*", "*",
"*",
     0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0,
0,
    "*", "*", "*", "*", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {"*",
"*",
    "*", 0, 0, 0, "*", "*", 0, "*", "*", "*", "*", 0, 0, 0, "*", "*", "*",
0}}

In[3]:=
trans[x_List] :=
  Module[{ratesPos = #[[1]] & /@
          StringPosition[StringJoin @@ ToString /@ x, "*0"]},
    runs = Split[x];
    class = If[runs[[1, 1]] == 0, 1, 2];
    If[class == 1, runs[[1]] = runs[[1]] /. (0 -> rates[[1]]);
      Do[runs[[2j + 1]] = runs[[2j + 1]] /. (0 -> rates[[ratesPos[[j]]]]),
{j,
           1, Length[ratesPos]}],
      Do[runs[[2j]] = runs[[2j]] /. (0 -> rates[[ratesPos[[j]]]]), {j, 1,
          Length[ratesPos]}]
      ];
    Join @@ runs /. "*" -> 0]

(in the above code, the variable "class" is used to determine if a row
starts with a 0 or not). You then map trans onto each row of the matrix
newMat:

In[4]:=
trans /@ newMat
Out[4]=
{{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}, {0, 0, 0, 37, 37, 37, 0, 0, 30, 0, 0, 0, 0, 20, 20, 20, 0, 0,
0,
    9}}

which, I hope, what you wanted to obtain.

Tomas Garza
Mexico City

----- Original Message -----
From: "Michael Loop" <loopm at yahoo.com>
To: mathgroup at smc.vnet.net
Subject: [mg29685] [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



  • Prev by Date: RE: Transforming matrices
  • Next by Date: Re: a problem with differentiation
  • Previous by thread: Transforming matrices
  • Next by thread: Re: Transforming matrices