Sliding window

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • epidemiologist
    New Member
    • Mar 2007
    • 10

    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
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    What have you tried so far? Anything? Whats a sliding window?

    Comment

    • epidemiologist
      New Member
      • Mar 2007
      • 10

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        What have you tried so far?

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          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

          Comment

          • epidemiologist
            New Member
            • Mar 2007
            • 10

            #6
            Hi Miller,
            I read the file like this:

            [CODE=perl]open(INPUT, "< x.txt") or die "open INPUT: $!" ;

            open (OUTPUT , "> y.txt") or die "open IOUTPUT: $!" ;;

            while(defined(m y $line=<INPUT>))
            {
            chomp($line);
            my @matrix = split("\t", $line);
            #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
            #e.g
            print OUTPUT ("I am supposed to print the average as being calculated"; "\t";"the othe individ average"; "\n");



            }

            close (INPUT) or die "close INPUT: $!";
            close (OUTPUT) or die "close OUTPUT: $!";

            ############### ############### ###
            ##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

            my $winsize = 7;
            for(my $i = 1; $i <= $len-($winsize-1)); $i++) {
            my $window = $seqobj->subseq($i,$i+( $winsize-1));
            }
            [/CODE]
            I hope you could help me (this is not easy for me)
            Thanks
            Last edited by eWish; Dec 25 '07, 02:06 AM. Reason: Added Code Tags

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              one way to do it:

              Code:
              use strict;
              use warnings;
              use Data::Dumper;
              my @AoA = ();
              my $i = 0;
              open(INPUT, "< x.txt") or die "open INPUT: $!";
              open (OUTPUT , "> y.txt") or die "open IOUTPUT: $!";
              
              # generate an array of arrays
              while (<INPUT>) {
                 chomp;
                 push @{$AoA[$i]},split(/\s+/);
                 $i++;
              }
              
              # next line is for debugging uncomment to see the data structure   
              # print Dumper \@AoA; 
              
              # loop through the array of arrays and do the math and print to OUTPUT 
              foreach my $x (0..$#AoA-1) {
                 foreach my $y (0..$#{$AoA[$x]}) {
                    print OUTPUT +($AoA[$x][$y]+$AoA[$x+1][$y])/2,"\t";
                 }
                 print OUTPUT "\n";
              }
              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.

              Comment

              • docsnyder
                New Member
                • Dec 2006
                • 88

                #8
                Code:
                foreach my $x (0..$#AoA-1) {
                should be

                Code:
                foreach my $x (0..$#AoA) {
                Greetz, Doc

                Comment

                • epidemiologist
                  New Member
                  • Mar 2007
                  • 10

                  #9
                  Originally posted by KevinADC
                  one way to do it:
                  Hi Kevin,
                  Many thanks for your help.
                  Ruby

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by docsnyder
                    Code:
                    foreach my $x (0..$#AoA-1) {
                    should be

                    Code:
                    foreach my $x (0..$#AoA) {
                    Greetz, Doc

                    No, that's not correct in this case: $AoA[$x+1]

                    Comment

                    • miller
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1086

                      #11
                      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
                      Code:
                      use strict;
                      use warnings;
                      
                      open(INPUT, "< x.txt") or die "open INPUT: $!";
                      open(OUTPUT, "> y.txt") or die "open OUTPUT: $!";
                      
                      my @lastLine = ();
                      while (<INPUT>) {
                      	my @input = m/(\d+)/g or next;
                          
                      	if (@lastLine) {
                      		my @averages = map {($input[$_] + $lastLine[$_])/2} (0..$#input);
                      		print OUTPUT join("\t", @averages) . "\n";
                      	}
                      	
                      	@lastLine = @input;
                      }
                      
                      close INPUT;
                      close OUTPUT;
                      x.txt
                      Code:
                      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
                      y.txt (The Output)
                      Code:
                      3	4.5	5.5	6	7.5	3.5
                      4.5	4	5	5	5	5.5
                      6	5	5.5	6	6	6
                      - Miller

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        That could be a good suggestion, especially if the file is very big, or use Tie::File to process the input file.

                        Comment

                        Working...