Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert square matrix to 3-column list

Posted on 2010-11-23
6
Medium Priority
?
333 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 2000 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:Justin Mathews
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

715 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