Solved

convert square matrix to 3-column list

Posted on 2010-11-23
6
323 Views
Last Modified: 2012-05-10
Hi. I have a symmetric square matrix (see attached file as sample). I need to convert it to a three column list (I only need either the upper triangular part or the lower triangular part since it is symmetric).
Can anyone give me a perl script that will do it? The actual matrix will be a csv file called similarityM.csv and will only contain the matrix.
With the attached matrix I would want to arrive at something like the following (the '...' mean etc)

allwineclub      amazing      -0.007007171
allwineclub      awesome      -0.005773905
…            
amazing      awesome      0.019403651
amazing      back      -0.001234014
…            
awesome      back      -0.00770258
…            


similarityM.csv
0
Comment
Question by:onyourmark
  • 3
  • 2
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 34195809
perl -F, -lane 'print "$F[0]\t$h[$_]\t$F[$_]" for $...$#h;@h=@F if !@h' similarityM.csv
0
 

Author Comment

by:onyourmark
ID: 34195985
Thanks I tried it but I got this:

C:\Users\Bill\Desktop\similarity>C:\Perl\bin/perl -F, -lane 'print "$F[0]\t$h[$_
]\t$F[$_]" for $...$#h;@h=@F if !@h' similarityM.csv
Can't find string terminator "'" anywhere before EOF at -e line 1.
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34198008
@ozo's solution above works fine. I gather you are running it on Windows. So just enclose the script in double quotes and escape the double quotes inside the script as:

perl -F, -lane "print \"$F[0]\t$h[$_]\t$F[$_]\" for $...$#h;@h=@F if !@h" similarityM.csv


BTW: This is not for points.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:onyourmark
ID: 34201365
Thanks!!!
0
 

Author Comment

by:onyourmark
ID: 34201404
Hi. Thanks very much. Could you explain a little of how this code works?
0
 
LVL 84

Expert Comment

by:ozo
ID: 34202108
-n  loops over input lines
-a  splits into @F
-F,  uses /,/ to split on
-l  removes line terminators from input and adds line terminators to the output
 for $. .. $#h  loops from the current line number to the index of the last entry in @h
@h=@F if !@h  gets the header names
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question