473,385 Members | 1,782 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,385 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 4401
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.