Highest value matrix parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Highest value matrix parsing
# 1  
Old 08-24-2015
Highest value matrix parsing

Hi All

I do have a matrix in the following format

Code:
   a_2  a_3 s_4 t_6
b  0 0.9 0.004   0
c 0 0 1 0
d 0 0.98 0 0
e 0.0023 0.96 0 0.0034

I have thousands of rows

I would like to parse the maximum value in each of the row and out put that highest value along the column header of that value. Following is the desired output file
Code:
b a_3 0.9
c s_4 1
d a_3 0.98
e a_3 0.96

Please let me know how to do this parsing using awk or sed
# 2  
Old 08-24-2015
Any attempts from your side?

---------- Post updated at 16:42 ---------- Previous update was at 16:35 ----------

Howsoever, try
Code:
awk '
NR==1   {for (i=1; i<=NF; i++) TT[i+1]=$i
         next
        }
        {MAX=$2
         COL=2
         for (i=3; i<=NF; i++) if ($i>MAX)      {MAX=$i
                                                 COL=i
                                                }
         print $1, TT[COL], MAX
        }
' file
b a_3 0.9
c s_4 1
d a_3 0.98
e a_3 0.96

# 3  
Old 08-24-2015
The shell fanatic is back. Somebody should time these examples (and maybe one using perl, also):
Code:
$ cat foodata
X a_2 a_3 s_4 t_6
b 0 0.9 0.004 0
c 0 0 1 0
d 0 0.98 0 0
e 0.0023 0.96 0 0.0034

$ ./foo.sh <foodata
b: a_3 0.9 (2)
c: s_4 1 (3)
d: a_3 0.98 (2)
e: a_3 0.96 (2)

$ cat foo.sh
#!/bin/bash
read header
columns=($header)
while read data; do
    line=($data)
    max=1 # index of first element
    for (( i=2 ; $i < ${#line[@]}; i++ )) ; do 
    if (( "$(echo ${line[$i]} '>' ${line[$max]} | bc -l)" == "1" )); then max=$i; fi
    done
  echo ${line[0]}: ${columns[$max]} ${line[$max]} "($max)"
done

Notes:
  1. I made a change to the first line of the datafile, so the headings lined up.
  2. Remove the last "($max)" if you don't need the index of the largest element
This is a fun exercise. Anyone want one in, like, SNOBOL?

---------- Post updated at 12:44 PM ---------- Previous update was at 12:30 PM ----------

I have 'way too much free time.
Code:
$ ./foo.pl <foodata
b    a_3    0.9
c    s_4    1
d    a_3    0.98
e    a_3    0.96

$ cat foo.pl
#!/usr/bin/perl -w
use strict;
my $debug=1;
my @headings=split ' ',<>;
while (<>) {
    chomp;
    my @line=split ' ',$_;
    my $max=1;
    foreach my $index ( 2 .. $#line ) { $max=$index if ($line[$max] < $line [$index]); }
    print $line[0]."\t".$headings[$max]."\t".$line[$max]."\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a subset of data from a large matrix

I do have a large matrix of the following format and it is tab delimited ch-ab1-20 ch-bb2-23 ch-ab1-34 ch-ab1-24 er-cc1-45 bv-cc1-78 ch-ab1-20 0 2 3 4 5 6 ch-bb2-23 3 0 5 ... (6 Replies)
Discussion started by: Kanja
6 Replies

2. Shell Programming and Scripting

Script read highest value

Hi All, Can someone help steer me to the simplest approach to write a script that reads the second column of a csv file and returns the highest value. example file: DATE,QUANTITY Mon Apr 30 01:56:00 2012,42 Mon Apr 30 02:24:00 2012,72 Mon Apr 30 02:56:00 2012,102 Mon Apr 30 02:59:00... (3 Replies)
Discussion started by: dekker74
3 Replies

3. Programming

Matrix parsing help !

Hello every body ! I'm a new in this forum and beginner in Perl scripting and I have some problems :(:(:(! I have a big file like that : ID1 ID2 Identity chromosome07_194379 chromosome01_168057 0.975 chromosome01_100293 chromosome01_168057 ... (23 Replies)
Discussion started by: mchimich
23 Replies

4. Shell Programming and Scripting

Printing highest value from a list

Hi all, I'm trying to get the item with the maximum value, and was wondering if someone can help me with it. Heres my input file: apples 15 books 15 books 17 pens 12 pens 15 umbrella 13Here's what my output file should look like: apples 15 books 17 pens 15 umbrella 13 Can... (2 Replies)
Discussion started by: r4v3n
2 Replies

5. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

6. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

7. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. UNIX for Dummies Questions & Answers

sort with highest wc

Hi :) I'm a unix beginner and i've recently got an assignment to write up a script to print the most common IP address that made requests from a webserver. I'm really lost in this one...and if someone could pls tell me where to start i'll be really greatful ! thanx (1 Reply)
Discussion started by: ymf
1 Replies
Login or Register to Ask a Question