Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I have two lists.

a = {{2, 2}, {3, 3}, {4, 4}, {5, 5}};
b = {1, 1};

I want to subtract a from b and want to get

{{1, 1}, {2, 2}, {3, 3}, {4, 4}}`

When I tried Subtract[a, b], I got an error. How do I fix this?

share|improve this question

marked as duplicate by Artes, Kuba, Mr.Wizard Apr 24 at 5:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
If you don't know start with Map, i.e. # - b & /@ a. –  Artes Apr 24 at 4:26
    
Basic issue as Artes has shown or duplicate of flexible threading –  Kuba Apr 24 at 5:00
    
Even better canditate for duplicate Extending listability to coordinates –  Kuba Apr 24 at 5:12

1 Answer 1

up vote 3 down vote accepted

Artes comment as an answer.

a = {{2, 2}, {3, 3}, {4, 4}, {5, 5}};
b = {1, 1};
(# - b)& /@ a
{{1, 1}, {2, 2}, {3, 3}, {4, 4}}

In the special case of your example, because of the way Mathematica defines addition of a scalar to a list, you can do

a - 1
{{1, 1}, {2, 2}, {3, 3}, {4, 4}}

a - x will work for any pair of the form b = {x, x}.

share|improve this answer
    
Maybe you could add: in this special case the same result can be achieved simply with a - 1. –  Jens Apr 24 at 5:10
    
@Jens. Good idea. Done. –  m_goldberg Apr 24 at 5:29

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