Nested loop question

Sujaya Neupane's Avatar

Sujaya Neupane

06 Jul, 2018 02:29 PM

Hi Chris,

I am a bit confused how list can be used to make something like a for loop.

In this example, how could I create a loop inside the existing loop with the same list as for_each_stim? I am not sure how the list {...} and for_each_stim{...} are related here.

var stim_index (scope = local; default_value = 0)

%define for_each_stim ()
    range_replicator (
        variable = stim_index
        from = 0
        to = num_stims - 1
        step = 1
        )
%end

protocol 'Mental Time Travel 2_images' {
    start_io_device (keyboard)

    block (nsamples = num_trials_per_stim) {
list (
            selection = random_without_replacement
            nsamples = num_stims
            sampling_method = samples
            ) {
            for_each_stim {
                trial {
                    task {
                        state 'Begin trial' {
                            target_stim_index = stim_index
...
...
...

Thanks,

--
Sujay

  1. Support Staff 1 Posted by Christopher Sta... on 06 Jul, 2018 03:15 PM

    Christopher Stawarz's Avatar

    Hi Sujay,

    As we discussed offline, the way to do this is to have one range replicator inside another, with a separate index variable for each:

    list (selection = random_without_replacement) {
        range_replicator (
            variable = outer_index
            from = 0
            to = num_stims - 1
            step = 1) {
            range_replicator (
                variable = inner_index
                from = 0
                to = num_stims - 1
                step = 1) {
                trial {
                    ...
                }
            }
        }
    }
    

    To omit the case where outer_index and inner_index are the same, include a test for this in the first state of your task:

    task {
        state 'Check for duplicate stim' {
            goto (
                target = 'Skip duplicate stim'
                when = outer_index == inner_index
                )
            goto ('Begin trial')
        }
        ...
        state 'Skip duplicate stim' {
            yield ()
        }
    }
    

    (Sorry, I couldn't think of a more elegant way to do this.)

    Cheers,
    Chris

  2. Christopher Stawarz closed this discussion on 09 Jul, 2018 01:56 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

07 Feb, 2019 02:24 PM
02 Feb, 2019 01:58 PM
01 Feb, 2019 06:25 PM
26 Jan, 2019 12:15 AM
18 Jan, 2019 05:44 PM