Skip to main content
added 112 characters in body
Source Link
Coolwater
  • 20.5k
  • 3
  • 39
  • 66

You could write:

listA = {0.3, 0.6, 0.6, 0.9};
listB = {0.1, 0.2, 0.3, 0.4};
Total[Map[listB[[#]] &, Nearest[listA -> "Index", listA, {∞, 10^-15}], {-1}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)

Or more simply:

Total[Nearest[listA -> listB, listA, {∞, 10^-15}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)

You could write:

listA = {0.3, 0.6, 0.6, 0.9};
listB = {0.1, 0.2, 0.3, 0.4};
Total[Map[listB[[#]] &, Nearest[listA -> "Index", listA, {∞, 10^-15}], {-1}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)

You could write:

listA = {0.3, 0.6, 0.6, 0.9};
listB = {0.1, 0.2, 0.3, 0.4};
Total[Map[listB[[#]] &, Nearest[listA -> "Index", listA, {∞, 10^-15}], {-1}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)

Or more simply:

Total[Nearest[listA -> listB, listA, {∞, 10^-15}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)
Source Link
Coolwater
  • 20.5k
  • 3
  • 39
  • 66

You could write:

listA = {0.3, 0.6, 0.6, 0.9};
listB = {0.1, 0.2, 0.3, 0.4};
Total[Map[listB[[#]] &, Nearest[listA -> "Index", listA, {∞, 10^-15}], {-1}], {2}]
(* {0.1, 0.5, 0.5, 0.4} *)