Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    52

    Exclamation Unanswered: transposing rows and columns

    Hi all.

    I am having a bad time with this problem. Could someone
    shed some light, please ?

    My program saves N numbers in a single line (usually there are more
    than 400 numbers on it). This is done M times, resulting in a matrix
    like the one below :

    1 4 10 -1 3 7 ...
    20 1 44 2 -10 5 ...
    2 5 50 2 0 1 ...
    ...
    ...

    How can I transpose this NxM matrix to MxN ?

    1 20 2
    4 1 5
    10 44 50
    -1 2 2
    3 -10 0
    7 5 1
    Thanks,

    Serg

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    nawk -f serg.awk file.txt

    Code:
    # with this infile:
    #
    # a b c d
    # e f g h
    # i j k l
    #
    {for(i=1;i<=NF;i++){a[i,NR]=$i};m=NF;n=NR}
    END{for(j=1;j<=m;j++){
             for(k=1;k<=n;k++){printf("%s ",a[j,k])}print ""}}
    
    # outputs:
    #
    #
    # a e i
    # b f j
    # c g k
    # d h l

  3. #3
    Join Date
    Feb 2004
    Posts
    52
    Thanks for the reply.

    It worked like a charm.

    Serg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •