LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2008, 03:55 PM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
awk convert column matrix to square matrix?


Hi.

I have a 1*(N*N) column matrix and I need to convert it to a N*N matrix - each successive N entries of the column matrix becoming a column of the square matrix.

the sqrt of $NR number of records gives the dimension of the square matrix. Can I somehow change the record separator such that I can extract each successive N entries of the column matrix?

I can then format the matrix myself - the extraction of the columns is my problem!

Any help appreciated!
 
Old 04-29-2008, 05:19 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you change the NF variable, you would probably simply drop the remaining fields.
I think different versions of awk behave differently when you change the NR variable. Especially if you increase it.

You could loop a variable from 1 to N*N and perform a modulas test ( NF % index ). If the result is zero, print \n.
Code:
echo >testfile {1..49}
~> echo >>testfile {1..36}
~> echo >>testfile {1..100}
~> awk 'BEGIN {ORS=" "}{ for (i=0; i<= NF; i++) {
                           if ((i%sqrt(NF) )==0) printf "\n"
                           print $(i+1)
                                                }}' testfile

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
I would have moved the sqrt() before the loop, but I was working interactively, which makes editing harder.
Code:
awk 'BEGIN {ORS=" "}{ dim=sqrt(NF)
                      for (i=0; i<= NF; i++) {
                           if ((i%dim )==0) printf "\n"
                           print $(i+1)
                                             }}' testfile
A test may be needed in case NF == 0, to avoid a division by zero error.

Last edited by jschiwal; 04-29-2008 at 06:22 PM.
 
Old 04-29-2008, 06:33 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I may have misread your original problem. If each record is in it's own line like:
1
2
3
...
49

Then you can change the RS & FS variable in the BEGIN block so that the record is read in.
Code:
 cat testfile2
1
2
3
.
.
.
49

 awk 'BEGIN {RS="";FS="\n";ORS=" "}{ dim=sqrt(NF); for (i=0; i<= NF; i++) {
                           if ((i%dim )==0) printf "\n"
                           print $(i+1)
                                                }}' testfile2

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49

Last edited by jschiwal; 04-29-2008 at 06:36 PM.
 
Old 04-30-2008, 12:16 PM   #4
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks - really appreciate your help!
 
Old 04-30-2008, 01:45 PM   #5
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Rep: Reputation: 0
hey thanks

i think i need this for this: http://www.linuxquestions.org/questi...e-bash-638748/

or not?

Thanks. When I am home i will try!

Last edited by NikosNL; 04-30-2008 at 02:18 PM.
 
  


Reply



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
Matrix Screensaver magicvash Linux - Software 11 10-28-2004 02:33 PM
Matrix Linux ryancoolest Linux - Newbie 2 09-13-2004 08:28 PM
matrix revolutions qanopus General 44 11-29-2003 05:33 AM
The Matrix eresc Linux - Hardware 4 11-28-2003 04:59 PM
Anyone seen The Matrix Reloaded rch General 20 05-30-2003 11:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 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