Lower triangular matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lower triangular matrix
# 1  
Old 12-10-2012
Lower triangular matrix

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.
# 2  
Old 12-10-2012
Code:
$ awk '{for (i=1;i<=NF;i++) if (i<=NR) printf $i FS}' infile
1 0 8 9 2 6 6 8 9 6 7 9 4 9 9 9 0 3 4 7 9

$ awk '{for (i=1;i<=NF;i++) if (i<NR) printf $i FS}' infile
0 9 2 6 8 9 7 9 4 9 9 0 3 4 7

These 2 Users Gave Thanks to rdcwayx For This Post:
# 3  
Old 12-11-2012
Thanks, it worked

---------- Post updated 12-11-12 at 10:57 AM ---------- Previous update was 12-10-12 at 08:52 PM ----------

Can these be done to appear in one column instead of one row? The matrix I have is a very large one.
Thanks
# 4  
Old 12-11-2012
Quote:
Originally Posted by iconig
Can these be done to appear in one column instead of one row? The matrix I have is a very large one.
Thanks
Replace FS with RS.
This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 12-11-2012
A bit shorter:
Code:
$ awk '{i=0;while (++i<=NR) printf $i FS}' file
1 0 8 9 2 6 6 8 9 6 7 9 4 9 9 9 0 3 4 7 9

$ awk '{i=0;while (++i<NR) printf $i FS}' file
0 9 2 6 8 9 7 9 4 9 9 0 3 4 7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Lower ASCII characters.

Hi, I'm writing a BBS telnet program. I'm having issues with it not displaying lower ASCII characters. For example, instead of displaying the "smiley face" character (Ctrl-B), it displays ^B. Is this because i'm using Ncurses? If so, is there any way around this? Thanks. (3 Replies)
Discussion started by: ignatius
3 Replies

2. Shell Programming and Scripting

Lower to upper..tr + awk ?

I have a file that has a pattern 2 lines, blanktwo line If encountering the first line, the 2nd line need to be converted to UPPERCASE...or...conver the 2nd line after ablank into uppercase Is there a 'tr' function in awk..(probably the best tool over sed) ? i.e. ......................... (6 Replies)
Discussion started by: stevie_velvet
6 Replies

3. Shell Programming and Scripting

Awk: Storing string in array(triangular form)

I have a string like below. Now i want to split this string like below and put the value in the array using awk. I am able to do it using bash but getting no clue for doing it in awk O/P A=0 A=01 A=014 A=0143 A=01438 A=014387 A=0143876 A=01438765 A=014387650 ... (7 Replies)
Discussion started by: siramitsharma
7 Replies

4. Shell Programming and Scripting

Upper and Lower case

Hi, I think this is a weird problem. I have two files...one with all UPPER case and the other one with a mix of upper and lower. Match each record in file1 against record in file2, if they match, then change the record in file1 to that of record in file2. Thanks in advance. (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

6. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

7. Programming

Triangular window

I want to code a triangular window in an array. The array size is an odd number and indices start from 1. For example Having the number of elements N = 13 The middle position 7, the value will be 1.0 Then things decrease to zero using a rectangular variation. Having problem how to code it. (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

awk - from arrays to lower triangular matrix

Hi, I have to reshape some ascii files. I have many ascii files that have collapsed the lower triangular of my matrix into row. I simply want to convert them back to the proper matrix form. So, my current data looks like this: 11 21 22 31 32 33 41 42 43 44 ... This is five columns of... (2 Replies)
Discussion started by: Viko Haestner
2 Replies

9. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

10. Shell Programming and Scripting

Upper And Lower Case

Hi! I pass a parameter to a script code and I have to make it upper case before use: $ MyShell aBcDe script code: UpperVariable=function($1) I can't know how make function, maybe some sed option? Thank You, PARIDE (1 Reply)
Discussion started by: pciatto
1 Replies
Login or Register to Ask a Question