LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 12-10-2012, 06:37 PM   #1
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Rep: Reputation: Disabled
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.
 
Old 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: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Regular expression needed to write [[:lower:]] to [[:lower:]] and [[:upper:]] rgdacosta Programming 13 04-05-2012 12:50 PM
gimp 2.2 or lower? dvdljns Linux - Software 2 10-17-2009 07:58 AM
Ram reported much lower than it should be daishin Linux - Laptop and Netbook 4 03-04-2007 11:10 PM
How do I lower security? njbrain Slackware 12 05-01-2005 12:51 PM
Lower resolution on 9.1? pprms Mandriva 1 09-07-2003 01:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration