0
$\begingroup$

Given a simple list as

list = Range[100]

and the output is

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, \
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, \
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, \
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, \
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}

assuming a var as window length as

wlen = 10

how to get a new list from the upper one with Map or MoivingMap, and the new list shall be like this

result = {{1}, {1, 2},{1, 2, 3}, ... ,
         {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
         {2, 3, 4, 5, 6, 7, 8, 9, 10, 11} , ... ,
         {90, 91, 92, 93, 94, 95, 96, 97, 98, 99},
         {91, 92, 93, 94, 95, 96, 97, 98, 99, 100}}

Thanks!

$\endgroup$
  • $\begingroup$ Did you try Subsets[list,wlen] ? $\endgroup$ – J42161217 Apr 22 '17 at 9:34
  • $\begingroup$ Subsets[list,wlen] does not give the result list but SystemException[MemoryAllocationFailure];) $\endgroup$ – Jerry Apr 22 '17 at 9:40
  • $\begingroup$ I raise the question for doing backtesting on stock prices list, where backtesting function could be applied on the sub-list with fixed length. $\endgroup$ – Jerry Apr 22 '17 at 10:05
1
$\begingroup$

If you want to use MovingMap, you can pad your array with some symbol and then substitute it by Nothing.

wlen = 10;
list = Range[100];
paddedArray = Join[ ConstantArray[None, wlen - 1],  list];
movingMapResult = MovingMap[Sequence, paddedArray, wlen - 1];
movingMapResult /. None -> Nothing

One-line version:

MovingMap[Sequence, ArrayPad[list, {wlen-1, 0}, None], wlen-1] /. None->Nothing

Using MovingMap Padding option:

MovingMap[Sequence, list, wlen-1, {Automatic, None}] /. None->Nothing
$\endgroup$
  • $\begingroup$ Working perfectly. Thanks! :) $\endgroup$ – Jerry Apr 22 '17 at 10:00
  • $\begingroup$ how to reach your level on MMA is another question ;) $\endgroup$ – Jerry Apr 22 '17 at 10:07
6
$\begingroup$

No need for anything fancy like MovingMap[]:

list = Range[100]; wlen = 10;
Partition[list, wlen, 1, -1, {}]
$\endgroup$
  • $\begingroup$ Working perfectly. Thanks! :) $\endgroup$ – Jerry Apr 22 '17 at 10:01
  • $\begingroup$ The upper Partition is beyond my understanding..;) $\endgroup$ – Jerry Apr 22 '17 at 10:15
  • $\begingroup$ sorry, I could only accept one answer. Thanks for your helping ! $\endgroup$ – Jerry Apr 22 '17 at 10:28
  • 2
    $\begingroup$ (+1) also: Partition[list, UpTo @ wlen, 1, -1] $\endgroup$ – kglr Apr 22 '17 at 10:37
1
$\begingroup$
Range[If[NonNegative[# - wlen], # - wlen-1, 1], #] & /@ list
$\endgroup$
  • $\begingroup$ Good, but If list is changed the other, the code shall be modified. $\endgroup$ – Jerry Apr 22 '17 at 9:47
  • $\begingroup$ @Jerry The edit is your after? $\endgroup$ – yode Apr 22 '17 at 9:51
  • $\begingroup$ not exactly, Range[If[NonNegative[#-wlen],#-wlen-1,1],#]&/@(Reverse@Range[100]) should begin with {100},{100,99}... $\endgroup$ – Jerry Apr 22 '17 at 9:55
  • $\begingroup$ sorry, I could only accept one answer. Thanks for your helping ! $\endgroup$ – Jerry Apr 22 '17 at 10:27
  • $\begingroup$ You are welcome,be my guest. :) $\endgroup$ – yode Apr 22 '17 at 10:34
0
$\begingroup$

i think this will work

list = Range[10];
wlen = 5;
consecutiveQ = Most[#] == Rest[#] - 1 &;
Select[Drop[Subsets[list, wlen], 1], consecutiveQ]
$\endgroup$
  • $\begingroup$ Subsets causes SystemException[MemoryAllocationFailure] here either. $\endgroup$ – Jerry Apr 22 '17 at 9:56
  • $\begingroup$ I made an edit for a smaller set. Is this the result you should get? $\endgroup$ – J42161217 Apr 22 '17 at 10:00
  • $\begingroup$ It works for Range[20] and wlen=10.... but for Range[100] the result is very big $\endgroup$ – J42161217 Apr 22 '17 at 10:03
  • $\begingroup$ yes, but if listchanged to Reverse@Range[10], the code is limited. $\endgroup$ – Jerry Apr 22 '17 at 10:09

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.