I am trying to write a program to transform some XYZ coordinate data from a 3D scanner into a uniform grid. The goal is to be able to add or subtract future scans from the current scan. To do that, I group all coordinate sets that fall within a particular interval (x and y) and then average the value of z for that cell. Here is a synthetic example:
m = RandomInteger[{0, 10}, {50, 3}]
tablem = Table[
Select[
m, #[[2]] <= (i + 1)*1 && #[[2]] > i*1 && #[[1]] <= (j + 1)*1 && #[[1]] > j*1 &], {i, 0, 9}, {j, 0, 9}];
This gives me a 10x10 matrix containing all set of coordinates matching the coordinate rule.
My problem is that in some case, more than one coordinate set matches the rule (see example below). I want to replace these XYZ sets by one set, with the same x and y values but where the z value is the average of the z in all sets.
Any idea how to fix this? Also, any suggestions on other options to transform the XYZ data into a grid are welcome!
NB: Apologies for the poor image quality! This is my first post here.
Thank you for the help!
Rached
ReplacePart[tablem, {i_, j_} /; Length[tablem[[i,j]]] > 1 :> {{j, i, Mean[tablem[[i, j]][[All, 3]]]}}]
should work. There are probably more succinct way to write it. $\endgroup$