How do add values in a vector using a sliding window?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do add values in a vector using a sliding window?
# 1  
Old 10-22-2013
How do add values in a vector using a sliding window?

Greetings.

I have a vector of numbers such as the following:
Code:
1
75
79
90
91
92
109
120
167
198
203
204
206
224
230
236
240
245
263
344
427
451
461
476
528
619
627
649
681
701
824
852
911
924
965
978
986

And I would like to know the number of values that fall within a certain sliding window, say 50. The result for the vector above, for a window-size of 50 is:

Code:
50	1
100	5
150	2
200	2
250	8
300	1
350	1
400	0
450	1
500	2
550	1
600	0
650	3
700	1
750	1
800	0
850	1
900	1
950	2
1000	3

Showing that there is 1 entry falling in the range 1-50, 5 entries in 51-100, 2 entries for 101-150, etc.

Whereas, for a window-size of 250 the results should look like:
Code:
250	18
500	6
750	6
100	7

I feel like there must be an easy one-liner for this, but I haven't been able to figure it out.

Thanks!
# 2  
Old 10-22-2013
awk -f twink.awk myFile
awk -v w=250 -f twink.awk myFile

twink.awk:
Code:
BEGIN {
  if (!w) w=50
}
{ a[int($0/w)]++ }
END {
  for (i in a)
    print (i*w)+w, a[i]
}

# 3  
Old 10-22-2013
Do you require a zero count when the range is empty?
# 4  
Old 10-22-2013
@Chubler_XL Yes, please.
# 5  
Old 10-22-2013
Try this slight change to vgersh99's post:

Code:
awk -v w=250 '
!w{w=50}
{
 v=int($0/w)
 a[v]++
 m=(m>v)?m:v
}
END{for(i=0;i<=m;i++) print w+i*w, 0+a[i]}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Toggle between xterm window and standard terminal window

Is it possible to toggle back and forth between an xterm invoked from one tty, and a shell invoked from a different tty? I am running Centos 7 with KDE and booting in non-graphic mode. After logging in on the default window (/dev/tty1) , I can then use ALT-F2 to access a new window (/dev/tty2),... (1 Reply)
Discussion started by: rhgscty
1 Replies

2. UNIX for Dummies Questions & Answers

Search and replace with a sliding window

Hi Unix Gurus, I have a file with data like: >header_1 TCCCCGA >header_2 CCAATTGGGTA The data to work with starts from the next line after '>header_xx'. (1) I want to search the three letter patterns 'CHH' or 'DDG' and replace C and G by exclamation ! so that CHH becomes !HH and DDG... (3 Replies)
Discussion started by: Fahmida
3 Replies

3. Shell Programming and Scripting

Sliding window for string manipulation

I have a sting of "0"s and "1"s that I need to analyze. I need to look at each "1" and determine if it is in a neighborhood that is enriched for "1"s which means it is one of at least three "1"s in a 4 character window. My desired output is a count of "1"s in an enriched area. For Example Input... (1 Reply)
Discussion started by: monstrousturtle
1 Replies

4. UNIX for Dummies Questions & Answers

"Sliding window" with variables

I'm doing a little work that involves computing the average completion time of the last 5 of many file decompressions. It's not too tough, but I'm wondering if maybe there's a better way to write it. This is a bash script; here's the current idea: ctime5=$ctime4 ctime4=$ctime3 ctime3=$ctime2... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Programming

vector c++

hello guys. i'm new to c++. i've problem using two dimensional vector. i've a project of making conway's game of life. this is the code that i have made so far. my problem is how can i give a two dimensional vector through main. glider.vec1 = vec; is not correct way to give a two... (2 Replies)
Discussion started by: nishrestha
2 Replies

6. UNIX for Dummies Questions & Answers

Sliding window

Very simple problem I am not able to solve. I have been trying to modify the following code: awk '{t=$1; c = x}{for (i = 1; i <= length; i += wn)print t FS"" substr($2, i, mx) > ("block" ++c)}' mx=100 wn=100 infile.txt What I am tryng to acccomplish, I have a bunch of files where the first... (3 Replies)
Discussion started by: Xterra
3 Replies

7. Shell Programming and Scripting

Sliding window for sequencing data

Hi! I have some sequencing data that I have aligned using maq software Now, I have data that looks like this each line is a 'tag' chr1 10001 chr1 10002 chr1 10005 chr1 10007 chr1 10008 chr1 10008 chr1 10008 chr1 10019 chr1 10019 chr1 10020 What I really want to find out is how... (1 Reply)
Discussion started by: biobio
1 Replies

8. Shell Programming and Scripting

Script help: sliding time windows

I have a script like this ... #!/bin/ksh database=$(echo $@ | sed 's/.*-S \(*\).*/\1/') instance=$(grep $database /var/opt/oracle/oratab | awk -F : '{print $1}') command=$(echo $@ | sed "s/$database/$instance/") echo $command if I execute this script in ksh or bash it works fine . ... (3 Replies)
Discussion started by: talashil
3 Replies

9. Red Hat

Maximizing X window without Window Switcher

Hi everyone! I have a strange situation. I'm running an NX remote Gnome desktop session. On the remote machine, there is a whole load of unsaved data in a window. A problem that I've been having with this NX session is that I can't load Gnome Applets, including the Window Switcher. So.. when I... (0 Replies)
Discussion started by: alexandicity
0 Replies

10. Windows & DOS: Issues & Discussions

window 2000 professional not pinging my window 98 system.

Hello, We someone help me resolve this problem. I have window 2000 professional, windows 98 and Unixware 7.0.1 on the network. I was able to establish connection with all. However, l was unable to ping window 98 from window 2000 professional. I was able to ping the window 2000 from the window... (10 Replies)
Discussion started by: kayode
10 Replies
Login or Register to Ask a Question