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.

The function Intersection takes a number of lists (sets) and finds their intersection. However, the precise input is Intersection[{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}] and not Intersection[{{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}}].

This means I am having difficulty automatising my procedure using Table. For example, I wish to first construct myInput=Table[i+j,{i,1,5},{j,1,8}] and then Intersection[myInput].

This does not work, and Flatten doesn't seem to be able to flatten at level 0.

Any help? Thanks!

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You need to use Apply.

Intersection @@ {{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}}

and

Apply[Intersection, {{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}}]

are two different notations for the same.

share|improve this answer
    
Oh great, thanks! Kinda strange though, isn't it. –  LBogaardt Jan 27 at 15:44
    
@LBogaardt Not at all. When you get used to Mathematica, Apply will be one of the fundamental tools you will use every day. This is a nice tutorial that's worth a look. –  Szabolcs Jan 27 at 15:48
    
Well, I mean, it's a bit strange 'Intersection' doesn't just accept a list of lists immediately, or ask at what level you wish to intersect: Intersection[{{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}}, 1] seems the most logical input to me. –  LBogaardt Jan 27 at 15:51
1  
@LBogaardt I guess it's because a set could have elements that are lists, and Apply solves the problem easily anyway. Recent versions of Mathematica have specialized functions for many small tasks, such as Total, but older versions were more minimalist and you had to implement things by combining more primitive functions. For example instead of Total[a] we used Plus @@ a. –  Szabolcs Jan 27 at 15:59
1  
@Szabolcs +1. One good technical reason for the introduction of Total is that Apply always unpacks. And in the past (before Total was introduced), there was no way around this. –  Leonid Shifrin Jan 27 at 16:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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