# Loss of 1 to 1 mapping due to duplicate in Association

The main goal of this procedure is to first rotate an array of 3D coordinate leaving z untouched,finding the positions of tuples with z larger than some value, and then mapping those positions to the un-rotated array.

I will provide an example to clear things up. Let the un-rotated array be test1 and the rotated one test2. Element #2 of test1 is {0.00195313, 0.00112764, 6.76149*10^-14} this gets rotated to {-0.00195313, -0.00112764, 6.76149*10^-14} in test2 at the same position,e.i. test2[[2]]. This rotated element may not be part of the original array, I used Nearest to find the closest element to this an then its corresponding position. The closest element in the original array is the one with coordinates X and Y {-0.00195313, -0.00112737} and its in position 131412. I am using the approach described here to select the elements I'd like to keep as it is the fastest method I've found. So after all this the element in position #2 of this filtered array should go to position 131412.

The problem seems to arise when using the nearest function, it might be mapping multiple elements in one array to one in the other one. I dont know how to get around this issue.

Here is my attempt at this:

test1 = Import["C:"path"jr12.csv", "CSV"];
test2 = Import["C:"path"Rjr12.csv", "CSV"];
test22d = test2[[All, {1, 2}]];
test12d = test1[[All, {1, 2}]];
index1 = PositionIndex[test12d];
n = Nearest[test12d];
pos = Join @@ index1 /@ (Join @@ (n /@ test22d));
ind = PositionIndex[pos];
outrot = UnitStep[0.1 - test2[[All, -1]]];
out = ConstantArray[1, Dimensions[outrot][[1]]];

result = Reap[Do[Sow[{out[[n]]*outrot[[ind[n]]]}], {n, 1,Dimensions[outrot][[1]]}]][[2, 1]];
(*Failure point*)
(*Part::pkspec1: The expression Missing[KeyAbsent,131157] cannot be used    as a part specification.
Part::pkspec1: The expression Missing[KeyAbsent,218453] cannot be used as a part specification
General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.*)

Dimensions[test2]
(*{261972, 3}*)
Dimensions[pos]
(*{261972}*)
Dimensions[ind]
(*{261801}  Loss of elements*)


Any advise or fix on this attempt would be appreciated.

Data is available here: test1 test2

• Is this what you are looking for? nearOriginal = Nearest[MapIndexed[Most@#1 -> First@#2 &]@test1]; test2[[Ordering@Ordering@nearOriginal[test2[[All, ;; 2]], 1]]]. Jul 3, 2018 at 0:16
• Why does result contain only zeros and ones? Jul 3, 2018 at 9:02
• @Coolwater The point of that list is to later multiply it by the z component of the original list, the reference explains the procedure, either keeping them intact or setting them to zero. Jul 3, 2018 at 16:12
• @Edmund the result Im looking for is just an array of 1s and 0s which are filtered in the rotated array but then mapped to the unrotated positions. Jul 3, 2018 at 16:38

So with a little more time I came up with something that solves the issue, but it is not that elegant, in my opinion.

test1 = Import["C:"Path"\\jr12.csv", "CSV"];
test2 = Import["C:\\Pat\\Rjr12.csv", "CSV"];
test22d = test2[[All, {1, 2}]];
test12d = test1[[All, {1, 2}]];
index1 = PositionIndex[test12d];
n = Nearest[test12d];
pos = Join @@ index1 /@ (Join @@ (n /@ test22d));
outrot = UnitStep[0.1 - test2[[All, -1]]];
out = ConstantArray[1, Dimensions[outrot][[1]]];
map = Reap[Do[Sow[{m, pos[[m]]}], {m, 1, Dimensions[pos][[1]]}]][[2,1]];
result = Reap[Do[Sow[out[[n]]*outrot[[map[[n, 2]]]]], {n, 1,Dimensions[outrot][[1]]}]][[2, 1]]


Or alternatively:

pos = Nearest[test1[[All, {1, 2}]] -> "Index", test2[[All, {1, 2}]], 1][[All, 1]];
result = UnitStep[0.1 - test2[[All, -1]]][[pos]];


I got rid of the problematic part by not using the PositionIndex and just creating a 2d array of coordinates which houses the position of the rotated array in the first index and the position to which it should be mapped on the second index,map. This DOESN'T solve the duplicate problem, just bypasses it. If anyone has more elegant solution feel free to share.

• Is result from that code incorrect? If not how is the problem not solved? Jul 3, 2018 at 16:44
• The result is correct, I just really wanted to solve the duplicate issue explicitly. Jul 3, 2018 at 16:49