473,219 Members | 1,544 Online

# Sliding window

Hi;
I am new to programming. I want to make a sliding window through colomns to claculate its average. For example
A B C D E F
2 4 5 6 9 0
4 5 6 6 6 7
5 3 4 4 4 4
7 7 7 8 8 8

if I have afile arranged in a similar way and way to calculate the average of each 2 rows for each individual, then slide one row below this. How can I do this.
Ruby
Mar 21 '07 #1
11 4393
4,059 Expert 2GB
What have you tried so far? Anything? Whats a sliding window?
Mar 21 '07 #2
I mean I will read the file in perl as usual. I want to read each line then in the next line I will add the next value in the column the previous. in the previous example I want to know (2+4)/2 , (4+5)/2,...till the end of the row.
In the next row, I want to know (4+5)/2, (5+3)/2, (6+4)/2,....

I am in deep need for any help,
thanks
Mar 21 '07 #3
4,059 Expert 2GB
What have you tried so far?
Mar 21 '07 #4
miller
1,089 Expert 1GB
Hi epidemiologist,

What you describe does not sound hard. It sounds like a very simple programming logic challenge. However, as Kevin asks, what have you tried so far?

You say that you will read the file in perl "as usual". Well, start there. Show us the code where you read in the file and start processing the data.

Regards,
- Miller
Mar 22 '07 #5
Hi Miller,
I read the file like this:

Expand|Select|Wrap|Line Numbers
1. open(INPUT, "< x.txt") or die "open INPUT: \$!" ;
2.
3. open (OUTPUT , "> y.txt") or die "open IOUTPUT: \$!" ;;
4.
5. while(defined(my \$line=<INPUT>))
6. {
7.     chomp(\$line);
8.  my @matrix = split("\t", \$line);
9. #I need here to insert the other part of the code to read the values, store it and #then add the next value in the same colum and print the the average on #another file
10. #e.g
11. print OUTPUT ("I am supposed to print the average as being calculated"; "\t";"the othe individ average"; "\n");
12.
13.
14.
15. }
16.
17. close (INPUT) or die "close INPUT: \$!";
18. close (OUTPUT) or die "close OUTPUT: \$!";
19.
20. #################################
21. ##this code should do this in a horizontal way while reading the file, I do not ##know what to do use the window size in avertical way
22.
23. my \$winsize = 7;
24. for(my \$i = 1; \$i <= \$len-(\$winsize-1)); \$i++) {
25.     my \$window = \$seqobj->subseq(\$i,\$i+(\$winsize-1));
26.                                                                          }
27.
I hope you could help me (this is not easy for me)
Thanks
Mar 23 '07 #6
4,059 Expert 2GB
one way to do it:

Expand|Select|Wrap|Line Numbers
1. use strict;
2. use warnings;
3. use Data::Dumper;
4. my @AoA = ();
5. my \$i = 0;
6. open(INPUT, "< x.txt") or die "open INPUT: \$!";
7. open (OUTPUT , "> y.txt") or die "open IOUTPUT: \$!";
8.
9. # generate an array of arrays
10. while (<INPUT>) {
11.    chomp;
12.    push @{\$AoA[\$i]},split(/\s+/);
13.    \$i++;
14. }
15.
16. # next line is for debugging uncomment to see the data structure
17. # print Dumper \@AoA;
18.
19. # loop through the array of arrays and do the math and print to OUTPUT
20. foreach my \$x (0..\$#AoA-1) {
21.    foreach my \$y (0..\$#{\$AoA[\$x]}) {
22.       print OUTPUT +(\$AoA[\$x][\$y]+\$AoA[\$x+1][\$y])/2,"\t";
23.    }
24.    print OUTPUT "\n";
25. }
26.
reference material:

Manipulating Arrays of Arrays in perl

print function

I hope I am not doing your school/class work for you. I consider that cheating and unethical, I hope you do too.
Mar 23 '07 #7
Expand|Select|Wrap|Line Numbers
1. foreach my \$x (0..\$#AoA-1) {
should be

Expand|Select|Wrap|Line Numbers
1. foreach my \$x (0..\$#AoA) {
Greetz, Doc
Mar 23 '07 #8
one way to do it:
Hi Kevin,
Ruby
Mar 23 '07 #9
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
1. foreach my \$x (0..\$#AoA-1) {
should be

Expand|Select|Wrap|Line Numbers
1. foreach my \$x (0..\$#AoA) {
Greetz, Doc

No, that's not correct in this case: \$AoA[\$x+1]
Mar 23 '07 #10
miller
1,089 Expert 1GB
Not that it matters anymore, but I would have chosen to do this in one step instead of saving the data to an intermediate data structure.

script.pl
Expand|Select|Wrap|Line Numbers
1. use strict;
2. use warnings;
3.
4. open(INPUT, "< x.txt") or die "open INPUT: \$!";
5. open(OUTPUT, "> y.txt") or die "open OUTPUT: \$!";
6.
7. my @lastLine = ();
8. while (<INPUT>) {
9.     my @input = m/(\d+)/g or next;
10.
11.     if (@lastLine) {
12.         my @averages = map {(\$input[\$_] + \$lastLine[\$_])/2} (0..\$#input);
13.         print OUTPUT join("\t", @averages) . "\n";
14.     }
15.
16.     @lastLine = @input;
17. }
18.
19. close INPUT;
20. close OUTPUT;
21.
x.txt
Expand|Select|Wrap|Line Numbers
1. A B C D E F
2. 2 4 5 6 9 0
3. 4 5 6 6 6 7
4. 5 3 4 4 4 4
5. 7 7 7 8 8 8
6.
y.txt (The Output)
Expand|Select|Wrap|Line Numbers
1. 3    4.5    5.5    6    7.5    3.5
2. 4.5    4    5    5    5    5.5
3. 6    5    5.5    6    6    6
4.
- Miller
Mar 24 '07 #11