The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix addition ( Row wise) gauravgoel Shell Programming and Scripting 3 05-17-2007 04:27 AM
Paste coulmn wise er_aparna Shell Programming and Scripting 3 06-26-2006 04:16 AM
Manipulating fields record wise rinku11 Shell Programming and Scripting 1 12-07-2005 10:42 AM
System RAM identification slot-wise diliphere UNIX for Advanced & Expert Users 0 08-23-2005 08:45 AM
listing files and directory in Page wise smdakram UNIX for Dummies Questions & Answers 1 01-12-2002 01:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
processing matrix column wise

I have a m X n matrix written out to file, say like this:

1,2,3,4,5,6
2,6,3,10,34,67
1,45,6,7,8,8

I want to calculate the column averages in the MINIMUM amount of code or processing possible. I would have liked to use my favorite tool, "AWK" but since it processes rowwise, getting the average of the first column values wud mean one call, getting the average of the second column values wud mean another call....so on till 'n' calls.

Is there a better way to this? You may also suggest a different way to represent the mXn matrix, as long I can conceptually map a mXn matrix to the suggested file format. Please help.
  #2 (permalink)  
Old 08-20-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -f rowAVG.awk myFile

rowAVG.awk:
Code:
BEGIN {
  FS=OFS=","
}
{
  for(i=1; i<=NF; i++)
    arr[i]+=$i
}
END {

  for(i=1; i<=NF; i++)
    printf("%.2f%s", arr[i]/FNR, (i==NF) ? "\n" : OFS)
}
some awk-s are more pesky than the others - not keeping track of the FNR and NF in the 'END' block. For those cases:
Code:
BEGIN {
  FS=OFS=","
}
{
  for(i=1; i<=NF; i++)
    arr[i]+=$i
  nf=NF; fnr=FNR
}
END {

  for(i=1; i<=nf; i++)
    printf("%.2f%s", arr[i]/fnr, (i==nf) ? "\n" : OFS)
}

Last edited by vgersh99; 08-20-2007 at 01:38 PM..
  #3 (permalink)  
Old 08-20-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
awk

hope this one can help you:

Code:
awk 'BEGIN{
FS=","
}
{
 for (i=1;i<=NF;i++)
 arr[i]=arr[i]+$i
 }
 END{
 for (j in arr)
 {
 temp=arr[j]/NR
 printf("The average of column %s is %s",j,temp)
 }
}' filename
  #4 (permalink)  
Old 08-21-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
risk of overflow?

First of all, thanks for the response.

The order of my matrix is curently 2000 X 1000....but it might increase over time. Is there a possibility of memory overflow? This is one reason why I didnt want to store many values in a code... preferable is if I could process them and output as they come, maintaining the minimum number of variables in the code.
  #5 (permalink)  
Old 08-21-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
the memory allocation is only limited by the physical/virtual memory configured on your box.
If you're on Solaris and using 'nawk' you would hit the limitation on the number of fields in a record much sooner than you'd notice the 'noticable' mmory aloocation issues.

If you can thing of the algorithm to achieve what you're trying to with out using a hash/array REGARDLESS of the implementation language, pls do share.

You might want post to USENET's comp.lang.awk - they're resourceful bunch!
  #6 (permalink)  
Old 08-21-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
I guess one way would be to invert the matrix and process a row (what used to be a 'column') in one shot producing avg.
Now... whether inverting the matrix and rprocessing the inverted matrix would be either quicker and/or less 'memory-consuming'.... that's a different issue.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0