Adding the individual columns of a matrix.

I have a huge matrix file containing some 1.5 million rows and 6000 columns. The matrix looks something like this:

1 2 3
4 5 6
7 8 9
3 4 5

I want to add all the numbers in the columns of this matrix and display the result to my stdout. This means that the numbers in the first column are:

1
4
7
3

When I add all of them up the result is 15 and in this way the sum of numbers present in the second column is 19 and third is 23. Hence my output should look like this:

15
19
23

I can add the numbers in the first column like this:

more matrix.mtx | awk '{sum+=$1} END {print sum}'

But don't know how to do it for the entire matrix of 6000 columns.

Try this ( don't know if your system will handle it ):

awk '{for (i=1;i<=NF;i++){a+=$i;if (i>max){max=i}}}END{for (j=1;j<=max;j++){print a[j]}}' file
3 Likes

Hi.

An available utility, numsum, part of num-utils:

#!/usr/bin/env bash

# @(#) s1	Demonstrate use of numsum.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C 

FILE=${1-data1}

pl " Data file $FILE:"
cat $FILE

pl " Results with numsum:"
/usr/bin/numsum -c $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39

-----
 Data file data1:
1 2 3
4 5 6
7 8 9
3 4 5

-----
 Results with numsum:
15 19 23

Information about numsum ( package available in the Debian repositories ):

COPYRIGHT
       numsum is part of the num-utils package, which is copyrighted by Suso
       Banderas and released under the GPL license.  Please read the COPYING
       and LICENSE files that came with the num-utils package

         Developers can read the GOALS file and contact me about providing
       submitions or help for the project.

MORE INFO
       More info on numsum can be found at:

       http://suso.suso.org/programs/num-utils/

-- excerpt from man numsum

Best wishes ... cheers, drl

3 Likes