Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Any way of shuffilng a slice of a loop table?

Hi everyone,

Does anyone know of a way to shuffle a slice within a loop? So, for example in a table, I have a column with the numbers 1 to 10. I want to mix up the numbers 1 to 5, and the numbers 6 to 10, but not simply mix all 10 numbers. In other words, the numbers will be randomised, but all the numbers below 6 will be somewhere in the first five rows, and likewise, numbers 6 and above will all be in rows 6 to 10.

Alternatively, is it possible to shuffle two colums identically (from a table containing, say, 4 columns) so that the pairs of items (one from each column) stay 'matched up'. So, if I have four columns, one contains A through to Z, and another contains 1 to 26, I can rearrange them randomly, but the row in the first column containing the letter A will still correspond to the row containing the number 1 in the second column and so on. The other two columns would stay in their original order.

Any advice gratefully welcommed!



  • Hi Neon,

    There are multiple ways to implement these things. With regard to the first question, you could simply split your loop item in two - as in, you'd use two loop items instead of one. The first loop item would contain numbers 1-5 and the second loop item would contain the rest.

    Regarding your second question, I think it would be best to place an inline_script at the start of your experiment, wherein you create a list variable that contains the item iterations for the to-be-matched columns:

      exp.your_list = range(0, 20) # if you'd have 20 different items

    For your two variables you'd also need lists that contain all possible values/items. Now, in the loop item, you'd just enter [variable_A[[your_list[0]]] in the first row for variable_A, [variable_A[[your_list[1]]] in the second row for variable_A, et cetera.



  • Hi Josh,

    Thanks so much for your super-quick reply. Unfortunately, I don't think splitting in to two lists will work in my situation because of the way I am doing things...

    To give more information: I need to alternate between two sets of items, so I have a single table with one column with one set of items, and another column with the other. The first part of the trial displays column1, then the second half of the trial displays column2. Although the rows are selected at random, obviously the two columns are 'paired', so if the first column was 1 to 10 and the second was A to J, then A would always follow 1, E would always come after 5 and so on. I know I can add 'shuffle column2' to mix things up, however there is a big problem with this. There is a third column which serves as a 'correct answer', which is used to judge whether the response is correct. So I essentially have a three column table, it is set up as follows:
    column1 (rows 1 to 5) and column2 (rows 1 to 5) correspond to column3 (rows 1 to 5 - which have "P" as the correct answer). Column1 (rows 6 to 10) and column2 (rows 6 to 10) correspond to column3 (rows 6 to 10 - which have "Q" as the correct answer). So basically I need to shuffle either column1 or column2 rows1 to 5, as these will all have identical answers in column3. If I did the shuffle as above, the answers would no longer be matched up. I'm not sure how I could use two lists, as the items from the list need to alternate in the trials - first item from list one, second from list two and so on until all of the items have been used.

    So the reason I asked about shuffling two columns identically was that I thought I could add another column and then have "first items" "correct answer for first items" "second items" and "correct answer for second items". Obviouly if I could make sure the answers column was shuffled in the same way as the items column, that would work.

    Does that make sense or have I just confused everyone even more?

    Top marks for anyone coming up with an elegant way of solving this one ;-)

    As usual, thanks a million in advance for any comments or suggestions.


  • edited January 2017

    Hi all,

    After hours of fiddling about with things (DataMatrix objects etc.) and trying various different things last night, I managed to find a way to achieve a way to shuffle a slice of a column in a loop table.

    With apologies to @sebastiaan for probably doing things that shouldn't ideally be done, I will detail this below, although what I have done might be a bit frowned-upon as it is perhaps not too sensible.

    In short, I added an inline script to the start of the experiment, during the prepare phase of which, I replace the slice of the appropriate column in the original (non-live) versions of the data table with a shuffled version of that slice.

    n.b. the loop is called "block_loop" and the column in question is called "SS2". I just wanted to re-order the first 5 items...

    from datamatrix import operations
    items['block_loop'].dm.SS2[0:5] = operations.shuffle(items['block_loop'].dm.SS2[0:5])

    Hopefully that makes sense in case someone needs to do something similar in the future. I'm always delighted to hear if there is a better/safer/more elegant way of doing things though!

    Best wishes,


    PS. when debugging and printing data matrix objects I had to install the module prettytable - this was a linux install built from scratch though, so maybe it is installed automatically with win/macOS.

  • Hi Neon,

    That seems like a perfectly sensible way to do it!


Sign In or Register to comment.