473,219 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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.
Thanks for your help,
Ruby
Mar 21 '07 #1
11 4393
KevinADC
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
KevinADC
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
KevinADC
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,
Many thanks for your help.
Ruby
Mar 23 '07 #9
KevinADC
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
KevinADC
4,059 Expert 2GB
That could be a good suggestion, especially if the file is very big, or use Tie::File to process the input file.
Mar 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Lance Kind | last post by:
Given: A really big table whose contents I want to view by "lazyloading" parts of it at a time. The entire database is too big to load all at once. Requirements: My UI is a window that shows...
1
by: Ryan | last post by:
What I want to do is run tcpdump continuously in the background and dump to a file. Then, while tcpdump is working, I'd like to grab arbitrary chunks of the open dump file and parse pieces at the...
2
by: Lonewolf | last post by:
Hi, does anyone know of a way to do sliding window similar to WMP 9's slider bar which can slide up when the mouse gets to the bottom of the screen in fullscreen mode during playback. Basically...
22
by: filia&sofia | last post by:
Let me rephrase my question that hasn't been answered properly. I want to read a file into n-bit chunks of data _extremely fast_, process the chunks and finally write them back into a different...
8
by: Noob | last post by:
Hello, Consider the following scenario. 'A' produces data which are sent in "packets" to 'B'. Each "packet" carries a sequence number, so that 'B' can insert the packet in the "right place" in...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.