4
$\begingroup$

I got the following list and I would like to loop and partition this list in the following way.

list={{1,2},{1,3},{1,4},{1,5},{2,1},{2,3},{2,4},{2,5},{3,1},{3,2},{3,2,4},{3,2,5},{4,1},{4,2},{4,2,3},{4,2,5},{5,1},{5,2},{5,1,3},{5,1,4}}

List at loop 1=

{{1,2},{1,3},{1,4},{1,5},{2,1},{2,3},{2,4},{2,5},{3,1},{3,2},{3,2},{3,2},{4,1},
 {4,2},{4,2},{4,2},{5,1},{5,2},{5,1},{5,1}}

List at loop 2=

{{},{},{},{},{},{},{},{},{},{},{2,4},{2,5},{},{},{2,3},{2,5},{},{},{1,3},{1,4}}

`

Essentially, the results in the first stage should only include the first two elements of each {}. The Second stage then should include the second and third elements of the {} and so forth. In case we see a {} with n elements the loop should continue.

Any ideas how I could achieve this easily?

Best,

Julian

$\endgroup$
4
$\begingroup$

Is this the idea?

list={{1,2},{1,3},{1,4},{1,5},{2,1},{2,3},{2,4},{2,5},{3,1},{3,2},{3,2,4},{3,2,5},{4,1},{4,2},{4,2,3},{4,2,5},{5,1},{5,2},{5,1,3},{5,1,4}}
MovingMap[Identity, #, 2] & /@ list // PadRight // ReplaceAll[#, {0, 0} -> {}] &

enter image description here

You could Transpose the result if needed.

Edit

MovingMap is new in version 10. A basic implementation is:

movingMap[f_, l_, w_] := f /@ Partition[l, w, 1]

after which you could just replace MovingMap above with movingMap.

It's not clear if you need the result to be rectangular. If not, I would do something like this:

#1 x + #2 y & @@@ Partition[#, 2, 1] & /@ list

where #1 x + #2 y & is a function that does whatever you need to each window.

enter image description here

$\endgroup$
  • $\begingroup$ is MovingMap a comment in Mathematica 8? I cannot find it. $\endgroup$ – Julian Oct 8 '14 at 16:14
  • 1
    $\begingroup$ Oh, no, it is new in version 10. But you could implement it like this: movingMap[f_, l_, w_] := f @@@ Partition[l, w, 1] $\endgroup$ – mfvonh Oct 8 '14 at 16:16
  • $\begingroup$ How would this work exactly? $\endgroup$ – Julian Oct 8 '14 at 16:19
  • $\begingroup$ MovingMap works like this: reference.wolfram.com/language/ref/MovingMap.html. Replace Identity with a function that does whatever you want to do to each window. The 2 following it is the width of the windows. The {0,0} is an assumption that let me use the short form of PadRight -- if you would ever actually have {0,0} sequentially in your lists, then you should instead use the fuller form of PadRight (in docs) that lets you specify what the padding is (and then you could get rid of the ReplaceAll) $\endgroup$ – mfvonh Oct 8 '14 at 16:25
  • $\begingroup$ Sorry, I meant implementing the movingMap function. So how would you suggest using it in your above code sampel? $\endgroup$ – Julian Oct 8 '14 at 16:30
2
$\begingroup$

I posted before reading other answers but now I feel a bit silly for not using Partition as mfvonh (implicitly) did. I propose:

PadRight[Partition[#, 2, 1] & /@ list, Automatic, {}]\[Transpose]
{

 {{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 3}, {2, 4}, {2, 5}, {3, 1}, {3, 2},
   {3, 2}, {3, 2}, {4, 1}, {4, 2}, {4, 2}, {4, 2}, {5, 1}, {5, 2}, {5, 1}, {5, 1}},

 {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {2, 4}, {2, 5}, {}, {}, {2, 3},
   {2, 5}, {}, {}, {1, 3}, {1, 4}}

}

(old answer)

You could use Part, suppress error messages with Quiet, then ReplaceAll Part[ . . . ] with {}:

pull[L_List, n_Integer] := Quiet[ #[[n ;; n + 1]] ] & /@ L /. _Part :> {}

Examples:

pull[list, 1]

pull[list, 2]
{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 3}, {2, 4}, {2, 5}, {3, 1}, {3, 
  2}, {3, 2}, {3, 2}, {4, 1}, {4, 2}, {4, 2}, {4, 2}, {5, 1}, {5, 2}, {5, 1}, {5, 1}}

{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {2, 4}, {2, 5}, {}, {}, {2, 3}, {2, 
  5}, {}, {}, {1, 3}, {1, 4}}

For all "pulls" perhaps:

Array[pull[list, #] &, Max[Length /@ list] - 1]
$\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.