Comparing two arrays of unequal length
You can use Tuples
to construct all pairs of positions and use Select
to pick the pairs that satisfy your condition:
pairs = Tuples[{ Range[Length[array1]], Range[Length[array2]]}];
Select[pairs, array1[[#[[1]]]] - 1 == array2[[#[[2]]]] &]
{{6, 2}, {8, 3}}
You can also use Outer
as follows:
Join @@ Outer[If[array1[[#]] - 1 == array2[[#2]], {##}, Nothing] &,
Range[Length[array1]], Range[Length[array2]]]
{{6, 2}, {8, 3}}
Yet other ways: variations on Roman's method using Position
and Outer
combination:
Position[1] @ Outer[Subtract, array1, array2]
Position[True] @ Outer[Equal, array1 - 1, array2]
Position[{i_, i_}]@Outer[List, array1 - 1, array2]
{{6, 2}, {8, 3}}
This is a general solution for any condition between the elements of array1
and array2
:
Position[Outer[List, array1, array2], {i_, j_} /; j == i - 1]
{{6, 2}, {8, 3}}
@kglr's solutions are simpler than this when the condition is a difference as in the given problem.