2
$\begingroup$

I'm trying to duplicate this behaviour:

{a,b,c}+1={a+1,b+1,c+1}

in two dimensions. In other words I want to have:

{{a,a},{b,b},{c,c}}+{1,1}={{a+1,a+1},{b+1,b+1},{c+1,c+1}}

but Mathematica gives me: "Objects of unequal length in {{a,a},{b,b},{c,c}}+{1,1} cannot be combined"

What's the neatest way of achieving this?

$\endgroup$
1
  • $\begingroup$ The case you request is as simple as {{a, a}, {b, b}, {c, c}} + 1 $\endgroup$
    – bill s
    Commented Dec 20, 2015 at 19:33

1 Answer 1

2
$\begingroup$

You can use Map for this:

Map[(# + {1, 1}) &, {{a, a}, {b, b}, {c, c}}]
(* {{1 + a, 1 + a}, {1 + b, 1 + b}, {1 + c, 1 + c}} *)
$\endgroup$
2
  • $\begingroup$ Thanks. Out of curiosity, do you know why Mathematica doesn't apply the same logic in the second case? What is it that makes the said operation ambiguous and make Mathematica hesitant to do the same as the 1D case? $\endgroup$
    – Shb
    Commented Dec 20, 2015 at 15:51
  • 2
    $\begingroup$ @Shb if you are adding e.g. a 2-vector to a 2×2 matrix, it is not clear along which dimension this addition should be performed. In your example it is not ambiguous, but because Mathematica does not have clear rules for all such ambiguous cases (unlike e.g. NumPy), for consistency it restricts threading to level 1. Although this situation could be improved by specifying new and more detailed threading rules, I think this cannot be changed now because it would affect (break) existing code. Also, as seen in the linked thread, choosing rules for ambiguous situations is not straightforward. $\endgroup$ Commented Dec 20, 2015 at 18:08

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