3

I have two Comma-separated values (csv) files. Each file has 1000 columns and 1000 rows ( i.e. every csv file form a matrix 1000x1000)

The values for every cell in the first csv file represent the mean.
The values for every cell in the second csv file represent the standard deviation.

I want to create new csv file (also 1000x1000). Every cell in this new csv file represent the coefficient of variation (CV) { cv=std/mean}

How can I divide the cells in the second csv file on the cells in the first csv file to get this new csv spreadsheet

For example ( just part of two columns form every csv file to clarify the issue):

First csv:
2 4 . . . .
2 4 . . . .
2 2 . . . .
2 8 . . . .
. . . . . .
. . . . . .

Second csv:
8 2 . . . .
8 4 . . . .
8 2 . . . .
8 2 . . . .
. . . . . .
. . . . . .

New csv:
4 0.5 . . . 
4 1 . . . .
4 1 . . . .
4 0.25 . . . 
. . . . . .
. . . . . .

PS. I can do it in Matlab. But I want to know how to achieve this under Unix/Linux commands.

  • Is octave an option? – steeldriver Jul 19 '16 at 0:51
  • I can do it in Matlab. but actually I want to know how to do it in Unix/Linux – user88036 Jul 19 '16 at 0:54
  • 1
    gnu datamash may be a viable option. – grochmal Jul 19 '16 at 1:29
2

If you count GNU octave as a "Unix/Linux command", then you could simply do

octave -qf << "EOF"
m = csvread("first.csv");
s = csvread("second.csv");
csvwrite("new.csv", s ./ m);
EOF
1

There were some interesting answers to a question of similar architecture a few days ago. Taking the preferred answer as a base, you can do

paste -d, file{1,2} | 
awk -F, '
{ for (i=1; i<=NF/2; i++)
    printf "%s%f", (i>1?", ":""), $(NF/2+i) / $i
  printf "\n" 
}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy