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
11 4401
What have you tried so far? Anything? Whats a sliding window?
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
What have you tried so far?
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
Hi Miller,
I read the file like this: - open(INPUT, "< x.txt") or die "open INPUT: $!" ;
-
-
open (OUTPUT , "> y.txt") or die "open IOUTPUT: $!" ;;
-
-
while(defined(my $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));
-
}
-
I hope you could help me (this is not easy for me)
Thanks
one way to do it: - 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.
- foreach my $x (0..$#AoA-1) {
should be - foreach my $x (0..$#AoA) {
Greetz, Doc
one way to do it:
Hi Kevin,
Many thanks for your help.
Ruby
- foreach my $x (0..$#AoA-1) {
should be - foreach my $x (0..$#AoA) {
Greetz, Doc
No, that's not correct in this case: $AoA[$x+1]
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 -
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 -
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) -
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
That could be a good suggestion, especially if the file is very big, or use Tie::File to process the input file.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |