Thursday 15 September 2011

perl - Count number of times an item is placed within a cell -


I randomly create a grid where the Cartesian coordinates 0 to 100 (100x100x100 grid) and "intensity" The point is generalized in 0 to 256. Here is a fraction of my code in Perl:

  open (file, $ file); While (sysread (FILE, $ data, 16)) {@ row = unpack ("f> 4", $ data); #Input file is binary so I had to convert $ x = int (($ line [0] - $ xmin) / ($ xmax - $ xmin) * 10) + 0.5); # Max and minimum variable $ y = int (($ line [1] - $ ymin) / ($ ymax - $ ymin) * 10) + 0.5); # Pre-defined $ z = int (($ line [2] - $ zmin) / ($ zmax - $ zmin) * 10) + 0.5); $ I = int (($ line [3] / 62 * 256) + 0.5); $ I = 255 if ($ i> 255); $ [$ X] [$ y] [$ z] = $ i; # Assigns the intensity for each data # point (in random order), only 1 point can be added to every 1x1x1 cell}   

Some points are very close together and The same 1x1x1 cell in them When this happens, each intensity overwrites the previous one.

Itemprop = "text">

You can easily do it with any other hash Just enter all of your keys ( $ x , $ y , $ Z ) and set the hash value together in one key < Whenever you enter a value for em> true

  my% visited_points; Open (file, $ file); While (sysread (FILE, $ data, 16)) {@ row = unpack ("f> 4", $ data); #Input file is binary so I had to convert $ x = int (($ line [0] - $ xmin) / ($ xmax - $ xmin) * 10) + 0.5); # Max and minimum variable $ y = int (($ line [1] - $ ymin) / ($ ymax - $ ymin) * 10) + 0.5); # $ Z = int (($ line [2] - $ zmin) / ($ zmax - $ zmin) * 10) + 0.5); $ I = int (($ line [3] / 62 * 256) + 0.5); $ I = 255 if ($ i> 255); My $ key = "$ x $ y $ z"; # Check if it is already in this cell if (exists ($ visited_points {$ key}) {# # Take some other action} $$ [$ x] [$ y] [$ z] = $ i; # Assigns the intensity for # every data # point (in random order), only 1 point # 1x1x1 can be added to each # of cells # The mark is that $ $ in the cell contains $ visited_points {$ key} = 1; }   

If you want to count, then you increase the value easily

No comments:

Post a Comment