Sum of range of rows and columns in matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of range of rows and columns in matrix
# 1  
Old 05-23-2013
Sum of range of rows and columns in matrix

Hi all,

I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all 25 columns of every 20 rows and escape 1 row after every addition.

For this, I was trying to use FOR loop. like
Code:
sum =0
for (i=1; i<=NF; i++)
{
for (j=1; j<=20; j++ )
{ sum =sum+$i
if j=20 {print sum} 
}
}

Please, help me to solve this.

Last edited by Franklin52; 05-23-2013 at 10:37 AM.. Reason: Please use code tags
# 2  
Old 05-24-2013
Could this help you /
Code:
awk '{if (j==0){j++;print;next}
for (i=1;i<=NF;i++) {
sum+=$i;}
j++;print $0
if (j == 21){print "SUM ----- "sum;j=0;}
}' matrixFile

# 3  
Old 05-24-2013
Hi pravin,

Thanks for your efforts.....

The code given by you is adding up entire matrix starting from top to every 20th row.
But, I want to add only 20 rows
starting from row # 2-21 and then
sum of row # 23 - 42, then
sum of row # 44-63... and so on.....

Also I would like to print the output in 21st column of 21st row, 21st column of 42nd row 21st column of 44th row and so on....instead of inserting a new row

Thanks in advance Smilie
# 4  
Old 05-24-2013
Code:
awk '{if (j==0){j++;print;next}
for (i=1;i<=NF;i++) {
sum+=$i;
}
j++;
if (j == 21){$21=sum;j=0;sum=0}
print $0}' MatrixFile

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 05-24-2013
Thanks Pravin Smilie
It worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Group by columns and add sum in new columns

Dear Experts, I have input file which is comma separated, has 4 columns like below, BRAND,COUNTRY,MODEL,COUNT NIKE,USA,DUMMY,5 NIKE,USA,ORIGINAL,10 PUMA,FRANCE,DUMMY,20 PUMA,FRANCE,ORIGINAL,15 ADIDAS,ITALY,DUMMY,50 ADIDAS,ITALY,ORIGINAL,50 SPIKE,CHINA,DUMMY,1O And expected output add... (2 Replies)
Discussion started by: ricky1991
2 Replies

2. Shell Programming and Scripting

Help with sum range of data set together

Input File: 2000 3 1998 2 1997 2 1994 1 1991 1 1989 1 1987 2 1986 2 1985 1 1984 1 . . 10 277256 9 278274 8 282507 7 284837 6 287066 5 292967 (4 Replies)
Discussion started by: perl_beginner
4 Replies

3. Shell Programming and Scripting

How to sum the matrix using awk?

input A1 B1 A2 B2 0 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 Output label A1 B1 A2 B2 A1 2 1 1 2 B1 1 2 2 1 A2 1 2 3 2 B2 2 1 2 3 Ex: The number of times that A1 and B1 row values are both 1 should be printed as output. The last row of A1 and B1 in the input match by having 1 in both... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

5. Shell Programming and Scripting

Get the SUM of TWO columns SEPARATELY by doing GROUP BY on other columns

My File looks like: "|" -> Field separator A|B|C|100|1000 D|E|F|1|2 G|H|I|0|7 D|E|F|1|2 A|B|C|10|10000 G|H|I|0|7 A|B|C|1|100 D|E|F|1|2 I need to do a SUM on Col. 5 and Col.6 by grouping on Col 1,2 & 3 My expected output is: A|B|C|111|11100 (2 Replies)
Discussion started by: machomaddy
2 Replies

6. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

7. Shell Programming and Scripting

Extracting rows and columns in a matrix based on condition

Hi I have a matrix with n rows and m columns like below example. i want to extract all the pairs with values <200. Input A B C D A 100 206 51 300 B 206 100 72 48 C 351 22 100 198 D 13 989 150 100 Output format A,A:200 A,C:51 B,B:100... (2 Replies)
Discussion started by: anurupa777
2 Replies

8. Shell Programming and Scripting

Extract values from a matrix given the rows and columns

Hi All, I have a huge (and its really huge!) matrix about 400GB in size (2 million rows by 1.5 million columns) . I am trying to optimize its space by creating a sparse representation of it. Miniature version of the matrix looks like this (matrix.mtx): 3.4543 65.7876 54.564 2.12344... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

9. Shell Programming and Scripting

Select columns from a matrix given within a range in BASH

I have a huge matrix file which looks like this (example matrix): 1 2 3 5 4 5 6 7 7 6 8 9 1 2 4 2 7 6 5 1 3 2 1 9 As one can see, this matrix has 4 columns and 6 rows. But my original matrix has some 3 million rows and 6000 columns. For example, on this matrix I can define my task as... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

10. Shell Programming and Scripting

reversing the rows and coloumns ina given matrix?

hi plz can any body giv ethe code to perform transpose matrix of agiven matrix in shell scripts program????? (1 Reply)
Discussion started by: chaitunanduri
1 Replies
Login or Register to Ask a Question