Re: Swiftest code to turn list of ordered pairs into Markov matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg29709] Re: Swiftest code to turn list of ordered pairs into Markov matrix
- From: "Orestis Vantzos" <atelesforos at hotmail.com>
- Date: Wed, 4 Jul 2001 03:08:33 -0400 (EDT)
- Organization: National Technical University of Athens, Greece
- References: <9hs1a0$bs9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
countPairsMatrix[oplist_, z_] :=
Module[{countRulz},
countRulz = ((# // First) -> Length[#]) & /@ Split[Sort[opList]];
countRulz = Flatten[{countRulz, {_, _} -> 0}];
Replace[Table[{i, j}, {i, z}, {j, z}], countRulz, 2]]
where oplist is your pairs_List and z the maximum value you refered to.
I ran it for z=50, Length[pairs]=500 and it took under a second.
By the way, the Fold oneliner you gave does not seem to work...I think it is
not supposed to work with matrices as input (the manual does not give a
levelspec argument, so Fold would feed your function with the rows of the
matrix instead of the elements!)
"Seth Chandler" <SChandler at Central.UH.Edu> wrote in message
news:9hs1a0$bs9$1 at smc.vnet.net...
> Suppose one has a list of ordered pairs oplist such as
> {{1,5},{1,6},{2,3},{2,3} ... }. All the values in the list are positive
> integers. The highest value in oplist is known to be some value z.
>
> Now one wishes to form a matrix such that the value at element i,j is
equal
> to
> Count[oplist,{i,j}]. One terribly slow way to do this is
>
> Table[Count[oplist,{i,j}],{i,1,z},{j,1,z}]
>
> A faster way to do this is as follows:
> Fold[ReplacePart[#,Extract[#,#2]+1,{#2}]&,Table[0,{z},{z}],oplist]
>
> Does anyone have a considerably faster way? A speed up of 100% or more
would
> be very helpful.
>
> For what it's worth Count[oplist,{something_,_}] is the same for all
values
> of something.
>
> P.S. The problem arises in converting a representation of a directed graph
> into a Markov transition matrix.
>
> Thanks,
>
> Seth J. Chandler
> Associate Professor of Law
> University of Houston Law Center
>
>
>
>