Solved

convert square matrix to 3-column list

Posted on 2010-11-23
6
322 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 165
Exchange 2010 Transport Rule Regex 28 108
Convert grep lines to perl 6 40
Perl string filter 5 79
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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