0
$\begingroup$

What does this error message mean?
The following code does work as expected, but it also gives the error message above.

Position[
    {{1, -1.1}, {-0, 8, 0.1}, {-1.1, 2}}, 
    _?(Min[DistanceMatrix[{#}, {{-1, 1}}]] > 2 &),
    1]

Out:{{2}, {3}}

Google returned nothing except the documentation for DistanceMatrix. The error message only happens when the function is used within the Position function, and when calculating distances to multi-dimensional vectors. The following two lines work without error messages:

Position[{1, -0.8, -1.1}, _?(Min[DistanceMatrix[{#}, {1}]] > 2 &), 1]
Out: {{3}}

(Min[DistanceMatrix[{#}, {{1, -1}}]] > 2 &)[{1, 0}]
Out: False

I'm using DistanceMatrix instead of EuclideanDistance in order to calculate the same thing for multiple vectors. I only used one here for brevity.

$\endgroup$
  • 1
    $\begingroup$ Are you aware of the difference between Position[..., {k}] and Position[..., k]? They are not the same thing for k != 1. $\endgroup$ – Szabolcs Mar 2 '17 at 17:07
  • $\begingroup$ Yeah I was aware, but I never explicitly made the connection that for k=1 they are equivalent. $\endgroup$ – ZeitPolizei Mar 3 '17 at 7:48
0
$\begingroup$

First: What does the error message mean?

The error message indicates, that DistanceMatrix doesn't know how it should calculate all the distances, because the list elements don't all have the same number of dimensions. You get the same error in the following two cases:

DistanceMatrix[{{2, 3}, 4}, {{1, 3}, 5}]
DistanceMatrix[{{2, 3}, {4, 2}}, {1, 3, 5}]

Second: Why does the error message occur?

Position by default also checks the Head of an expression, which is at index 0. For example:

Position[{1, -0.8, -1.1}, _?(Min[DistanceMatrix[{#}, {1}]] < 2 &), 1]
Out: {{0}, {1}, {2}}

…because for some reason the distance between a symbol and and any number is always 1 in DistanceMatrix. See:

DistanceMatrix[{List}, {1, 3, -2, 0}]
Out: {{1, 1, 1, 1}}

So to avoid the error message you can either add the Heads->False Option like so:

Position[
    {{1, -1.1}, {-0, 8, 0.1}, {-1.1, 2}}, 
    _?(Min[DistanceMatrix[{#}, {{-1, 1}}]] > 2 &),
    1,
    Heads->False]

Or you can specify the Head in the Pattern like so:

Position[
    {{1, -1.1}, {-0, 8, 0.1}, {-1.1, 2}}, 
    _List?(Min[DistanceMatrix[{#}, {{-1, 1}}]] > 2 &),
    1]

See also here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.