LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 11-23-2011, 11:25 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line


Dear All,

I am asking if it is possilbe to get the nth from a file then apply the a matrix dot multiplication on that line. The matrix is from another file.

I have a xyzfile with lines like:
Code:
x1 y1 z1
x2 y2 z2
x3 y3 z3
...
...
x12 y12 z12
x13 y13 z13
...
...
I have a matrix saved in file called matrix with lines:
Code:
a11 a12 a13
a21 a22 a23
aa31 a32 a33
I found I can do dot multiplication between a matrix and a list of coordinates by:
Code:
awk 'NR==FNR {
        for(i=1;i<=NF;i++)A[NR,i]=$i
        next
      }
      {
        for(i=1;i<=NF;i++){
          t=0
          for(j=1;j<=NF;j++)
            t+=A[i,j]*$j
          printf ("%f%s", t, FS)
        }
        print ""
      }' matrixfile xyzfile
This will do the do multiplication line by line until the end of xyzfile.

However, my question is I want to dot multiply only the No.i line in the xyzfile by the matrix.

I do not want to use another awk and then redirect to a file include that line, then multiply it with matrix by the above code. I do not want any redundant file generated. I hope to do everything in a single awk simply and the running speed should be fast.

How could I change the above code slightly to achieve my goal? Is that possible to use getline in this case?

I would thank you for your kind help!!!

Last edited by cristalp; 11-23-2011 at 11:26 AM.
 
Old 11-23-2011, 11:47 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
However, my question is I want to dot multiply only the No.i line in the xyzfile by the matrix.
Please explain further? Or at least how this differs from using FNR?
 
Old 11-23-2011, 12:03 PM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Please explain further? Or at least how this differs from using FNR?
OK, but I am worried that will make it more complicated.

The point is that the matrix file is generated from the No.i(th) line of the xyzfile. So, for each line in xyzfile the matrix will be different!!! So, I can not simply apply a single matrix to every line of the xyzfile. I must use THAT matrix generated by THAT line in the xyzfile dot multiply on THAT line itself!!! Do you get the point?? Thanks!!!

Last edited by cristalp; 11-23-2011 at 12:16 PM.
 
Old 11-23-2011, 12:38 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by cristalp View Post
OK, but I am worried that will make it more complicated.

The point is that the matrix file is generated from the No.i(th) line of the xyzfile. So, for each line in xyzfile the matrix will be different!!! So, I can not simply apply a single matrix to every line of the xyzfile. I must use THAT matrix generated by THAT line in the xyzfile dot multiply on THAT line itself!!! Do you get the point?? Thanks!!!
Looks like I am a bit slow on comprehending. So do you mean that the matrix starts at line 'i'? You do know that matrices can vary in dimension, right? Even "normal" vectors can be viewed as matrix. The dot product of two vectors
Code:
a1     b1
a2  *  b2  = a1*b1 + a2*b2 + a3*b3
a3     b3
can be interpreted as matrix multiplication of
Code:
                b1
( a1 a2 a3 ) *  b2
                b3
So, from your example I would guess that we are talking about three dimensional matrices. Is that correct? So when you say the matrix that start at line 'i' you actually mean the matrix that spans from line 'i' to line 'i+2', right?
Quote:
I must use THAT matrix generated by THAT line in the xyzfile dot multiply on THAT line itself!
Which means that line 'i' must be replaced? What about the other two (if my assumption about three dimensions is correct) lines?
Please give an example where your matrix is marked (bold) in the first file and how the computation would look like on *paper*. If one file is indeed to be edited then show this result, too.
 
  


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] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
using awk to select a line in a file edwardcode Programming 2 10-05-2011 12:05 PM
Can someone help me awk this line out of a file - boyd98 Programming 3 02-12-2010 02:57 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM


All times are GMT -5. The time now is 05:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration