Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi. Im actually not sure if this is more a problem for excel but I'm not getting far with that so i thought id give this a shot. I have two columns of data that look like this;

AAvAE 3

AAvAH 1

AAvAO 2

AAvAW 2

AAvAY 3

AAvB 10

AAvCH 10

...

ZvTH 8

ZvUH 10

ZvUW 10

ZvV 6

ZvW 8

ZvY 9

ZvZH 2

the idea is that ZvV represents the point (z,v) in a matrix where the columns and rows are titled with these 'names' AA through to ZH (f.y.i these are phonemes). So i want to transfer the data from this form into the matrix form, with the follwing requirement;

A point in the matrix x,y (XvY) equals y,x (YvX), the values for XvY and YvX that i have should be averaged and incorporated into both of these points of the matrix.

The data that I have is for every phoneme combination (Phoneme1vPhoneme2), some cases i have multiple results. -thanks (steamerboy - not logged in because password is hard to remember plus my email account is not working for apparently no reason *shrug*)

Replies are listed 'Best First'.
Re: constructing a matrix with perl
by chas (Priest) on Feb 28, 2005 at 17:20 UTC
    while(<>){ ($coords,$entry)=split; ($coord1,$coord2)=split 'v',$coords; $M{$coord1}{$coord2}=$entry; } print $M{AA}{CH},"\n"; #just to test
    chas
    (I'm not sure what you meant about "multiple results" or what you wanted to do in that case, so I didn't consider that.)
    (Update: My apologies - I started thinking about the question and forgot to make make the matrix "symmetric" as you asked; however, I'm a bit confused about exactly how you want this handled because the matrix doesn't seem necessarily square (or am I misunderstanding?) You could certainly make a new matrix in a similar way, replacing various xy entries by (xy entry + yx entry)/2 if both entries exist. But what if they don't both exist?)
      yeah it should be a 39 by 39 square. there are 1,400 odd numbers - in my data - i suppose its symetrical accross a diagonal line of zeros, AAvAA, AEvAE.... ZHvZH.

      I havent included all the values here.

      I haven't actually got data for XvX pairs, where the values are zero - but it will be zero for the matrix. I do have all cases of YvX for any XvY.

      hope that clears things up - any more questions ask away! -thanks

        In that case, I would probably first create the square "matrix" with all entries initialized to 0. Then I would read in the data you have as in my previous reply. Finally, I would loop over the entries making them symmetric. To do this, you could make an array @A with entries AA,...,ZH and then do the symmetrizing over the matrix entries $M{$A[i]}{$A[j]} where i<j.
        AA,...,ZH includes 208 values, doesn't it? So it seems like your matrix is much bigger than 39 by 39. But I may be misunderstanding something still.
        It is probably possible to do all this in fewer steps, but there probably isn't much change in efficiency.
        (I am getting disconnected a lot, so I may stop posting till later.)
        chas