austinby has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to bin the 2D data shown below in a grid. i.e. (x1,y1), (x1,y2),...(x1,yn), (x2,y1), (x2,y2),...(x2,yn), etc.
FYI this is not a school assignment, etc.
Here's how I am proceeding:
1). Create an array of x-values (@egvx)
2). Create an array of y-values (@egvy)
3). Get the minimum and maximum values of each array ($minx, $maxx, $miny, $maxy)
4). Create two ranges: @rgx = ($minx..$maxx) and @rgy = ($miny..$maxy) with regular spacings of 0.1.
5). For each value in @egvx, loop through all the values in @egvy and check where each pair of values lies by looping through bins defined by ($rgx[$x] and $rgx[$x] + 0.1), and ($rgy[$y] and $rgy[$y] + 0.1)
6). If a pair of @egvx and @egvy values falls between ($rgx[$x] and $rgx[$x] + 0.1), and ($rgy[$y] and $rgy[$y] + 0.1) increase the count of that bin.
Steps five and six are the problem areas. First, I have 196 bins, but after running the script, I get only 90 bins. Secondly, the bin counts are all zero.
It's a pretty long code. I have shown only that section where I try to handle steps 5 & 6.
my $j1x = 0; my $j1y = 0; my $k = 0; my $ctxy = 0; #create the number of bins while($j1x < $kx) { while($j1y < $ky) { $bin[$ctxy] = "bin\t$num"; $binCnt[$ctxy] = 0; $num++; $ctxy++; $j1y++; } $j1y = 0; $j1x++; } #@egvx = x-values, @rgx = range of x-values, @binx = x-bins #@egvy = y-values, @rgy = range of y-values, @biny = y-bins #@upy and @upx = upper-boundaries of yth and xth bins my $j = 0; my $x = 0; my $y = 0; my $c = 0; open(TST,">test.txt"); while($j < $i) #loops through the x-values { while($c < $i)#loops through the y-values { while($y < $ky)#loops through the y-bins { $upy[$y] = $rgy[$y] + 0.1; while($x < $kx)#loops through the x-bins { $upx[$x] = $rgx[$x] + 0.1; #check which bin the x-value falls into if(($egvx[$j] > $rgx[$x])and($egvx[$j] < $upx[$x])) { #print TST "$egvx[$j]\t$rgx[$x]\t$upx[$x]\n"; #record x-center of the bin $binx[$x] = ($rgx[$x] + $upx[$x])/2; #check which bin the y-value falls into if(($egvy[$c] > $rgy[$y])and($egvy[$c] < $upy[$y])) { #record y-center of the bin $biny[$y] = ($rgy[$y] + $upy[$y])/2; $binCnt[$x] += $binCnt[0]; print TST "$binCnt[$x]\n"; #print TST "x: $egvx[$j] $rgx[$x] $upx[$x]\t y: $egvy[$j]\ +t$rgy[$y]\t$upy[$y]\n"; } } $x++; } $x = 0; #reset x-bins $y++; } $y = 0; #reset y-bins $c++; } $c = 0; #reset y-values until all x-values have been tested $j++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 2D binning
by BrowserUk (Patriarch) on Feb 18, 2010 at 23:58 UTC | |
|
Re: 2D binning
by spx2 (Deacon) on Feb 19, 2010 at 00:08 UTC | |
by austinby (Initiate) on Feb 19, 2010 at 13:32 UTC | |
by austinby (Initiate) on Feb 19, 2010 at 16:36 UTC | |
by BrowserUk (Patriarch) on Feb 19, 2010 at 16:59 UTC | |
by spx2 (Deacon) on Feb 19, 2010 at 17:01 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |