LinuxQuestions.org lower triangular
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

 12-10-2012, 06:37 PM #1 iconig LQ Newbie   Registered: May 2012 Posts: 28 Rep: lower triangular Hi, I was wondering if there is a way of using awk to extract the lower or upper triangular matrix from a symmetric matrix. Say I have this matrix: \$ cat data: Code: ```1 0 9 6 7 9 0 8 2 8 9 0 9 2 6 9 4 3 6 8 9 6 9 4 7 9 4 9 9 7 9 0 3 4 7 9``` I am trying to do two things. Result 1: Code: `0 9 2 6 8 9 7 9 4 9 9 0 3 4 7` Result 2: Code: `1 0 8 9 2 6 6 8 9 6 7 9 4 9 9 9 0 3 4 7 9` Result 1 has only the lower triangular without the diagonal while result 2 has the lower triangular and the diagonal. Thanks.
 12-10-2012, 10:36 PM #2 btmiller Senior Member   Registered: May 2004 Location: In the DC 'burbs Distribution: Arch, Scientific Linux, Debian, Ubuntu Posts: 4,290 Rep: I haven't thought it out fully, but it would be a lot easier if you store the matrix row-wise instead of column wise. I've no doubt that this could be done with awk, but I'd probably use a small Perl or Python script to store it row-wise, e.g.: Code: ```#!/usr/bin/env python import sys n = 0 r = [] for line in sys.stdin: line = line.strip() larr = line.split() n += 1 r.extend(larr[len(larr)-n:]) print r``` Note that this is totally untested. Error checking and extending this code to not store the diagonal is left as an exercise to the reader. It should be fairly straightforward to transfer this to awk, for someone who knows awk syntax a bit better than I do :-). Last edited by btmiller; 12-10-2012 at 10:38 PM. Reason: oops ... code typo...

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post rgdacosta Programming 13 04-05-2012 12:50 PM dvdljns Linux - Software 2 10-17-2009 07:58 AM daishin Linux - Laptop and Netbook 4 03-04-2007 11:10 PM njbrain Slackware 12 05-01-2005 12:51 PM pprms Mandriva 1 09-07-2003 01:52 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:22 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -