473,915 Members | 4,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sliding window

10 New Member
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 4448
KevinADC
4,059 Recognized Expert Specialist
What have you tried so far? Anything? Whats a sliding window?
Mar 21 '07 #2
epidemiologist
10 New Member
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 Recognized Expert Specialist
What have you tried so far?
Mar 21 '07 #4
miller
1,089 Recognized Expert Top Contributor
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
epidemiologist
10 New Member
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 Recognized Expert Specialist
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
docsnyder
88 New Member
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
epidemiologist
10 New Member
one way to do it:
Hi Kevin,
Many thanks for your help.
Ruby
Mar 23 '07 #9
KevinADC
4,059 Recognized Expert Specialist
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

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

Similar topics

0
1929
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 ten records at a time. It pages up and down. Initialy it loads with the first ten entries. I push page down and the next ten show up. I can back up and look at the previous ten. Right now, I'm just shooting for readonly access.
1
1978
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 head of the file. After I'm done parsing I'd like to delete them from the front of the file, creating a sort of sliding window effect. What would be the best way to do this? -ryan
2
4778
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 I'm trying to achieve the same effect in my directshow program done in C# via managed assembly. I don't know of any control which can do this kind of sliding. can anyone enlighten me ?
22
4008
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 file. The problem is that usually people suggest algorithms that are slow. I'm thinking that the possible algorithm should first read the file into a buffer (in computer memory), which is large w.r.t. length of the individiual chunk of bits....
8
2693
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 a sorted list. The sequence number of the 1st packet is 0. The sequence number of the 65536th packet is 65535. The sequence number of the 65537th packet "wraps around" back to 0.
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.