Adding or substracting from a Matrix column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding or substracting from a Matrix column
# 1  
Old 09-17-2012
Adding or substracting from a Matrix column

Hello!

I'm new to linux programming. It would be great if you could help me out.

I have a matrix of kind:

10 30.0
20 190.5
40 180.
50 320.5

I would like
to substract 180 from column 2 If the value is >180
to add 180 for column 2 If the value is <180
nothing if it is equal to 180.

The output should look like

10 210.0
20 10.5
40 180.
50 140.5

Thank indeed for the help.
# 2  
Old 09-17-2012
Code:
awk '{t=$2}$2>180{t-=180}$2<180{t+=180}{$2=t}1' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-17-2012
Thanks for the prompt helpful reply.

I have saved my matrix in a file a.dat and the script file as pm180

#!/bin/bash

cat $1 | awk '{t=$2}$2>180{t-=180}$2<180{t+=180}{$2=t}1' >file

and excuted the script as follows:

pm180 a.dat

Sucessfuly excuted!

Last edited by lovelinux; 09-17-2012 at 12:38 PM..
# 4  
Old 09-17-2012
Try this:
Code:
awk '{$2=(($2==180?0:$2)+180)%360; print}' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transpose matrix based on second column using awk

Hi, Is it possible to transpose the matrix like this using awk ? Many thanks in advance Input abc Name_1 0 abc Name_2 1 abc Name_3 2 abc Name_4 0.4 def Name_1 0 def Name_2 9 def Name_3 78 def Name_4 1 Output abc def Name_1 0 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

2. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

3. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

4. Shell Programming and Scripting

column to matrix

Hello All, I need your help in the following problem. I have a matrix of 500 columns and 1000 rows and in each cell, it is having a value range from 0 to 9. I would like to convert each column in to a matrix, according to the value in each cell (ie) 0 to 9. For each column, I need a matrix... (5 Replies)
Discussion started by: Fredrick
5 Replies

5. Shell Programming and Scripting

3 column .csv --> correlation matrix; awk, perl?

Greetings, salutations. I have a 3 column csv file with ~13 million rows and I would like to generate a correlation matrix. Interestingly, you all previously provided a solution to the inverse of this problem. Thread title: "awk? adjacency matrix to adjacency list / correlation matrix to list"... (6 Replies)
Discussion started by: R3353
6 Replies

6. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

7. Shell Programming and Scripting

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: ... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

8. Shell Programming and Scripting

Extracting columns from a matrix and storing each column in a separate file

Hi All, I have a huge matrix file consisting some some millions rows and 6000 columns. The contents are just floating point numbers in the matrix. I want to extract each column (i.e. 6000 of them) and store each column in a separate file. For example, 1.dat will consist of elements from column... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

9. Shell Programming and Scripting

two-column data to matrix in AWK

Howdy, I need to convert an association data matrix, currently in a two-column format, into a matrix with numbers indicating the number of associations. I've been looking around for AWK code in the list, but could not find anything. Here's an example of what I want to perform: original... (10 Replies)
Discussion started by: sramirez
10 Replies

10. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: Abhishek Ghose
5 Replies
Login or Register to Ask a Question