Solved

convert square matrix to 3-column list

Posted on 2010-11-23
6
321 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now