Hi perlmonks,

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.

X Y
3.70722 0.54617
3.67342 0.64892
3.75017 0.85075
3.82831 0.83506
3.71852 0.79251
3.80706 0.70426
3.91839 0.94094
3.90726 0.81722
3.89007 0.76107
3.98006 0.62477

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++; }

In reply to 2D binning by austinby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.