Solved

convert square matrix to 3-column list

Posted on 2010-11-23
6
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans

729 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