Randomize location by trial & reshuffle location if condition not met

1 view (last 30 days)
I have the following code that allows me to present a stimulus at a location randomly selected from a list of X,Y coordinates 10 times before changing to another randomly selected location. I would like it to 1. present the stimulus at each location 10 times but randomize the location every trial (e.g. Trial 1: X1,Y1; Trial 2: X3, Y3; Trail 3: X7,Y7; Trial 4: X1,Y1; etc. trial count at X1,Y1 = 2 out of 10, all others = 1 out of 10) & 2. reshuffle the tested location into the pool if a condition is not met prior to presentation.
I am thinking this may require me to create a list of all potential trials and have the program randomly pull from that list. Any thoughts or advice would be appreciated.
for k = 1 : numel(X_vector)
n_trials = 10;
if strcmp(Testing_Mode,'random')
idx=randperm(length(X_vector),42);
x = X_vector(idx(k));
y = Y_vector(idx(k));
end
end
Unrecognized function or variable 'X_vector'.

Answers (1)

Divyanshu
Divyanshu on 17 May 2023
Hi HumbleCoder,
Few pointers to consider for better understanding of the sample script:
  • The demo script below takes 6 sample location points and number of trials for each location is 5.
  • The script makes use of a dictionary where the keys represent the location which is a string constructed using x & y coordinates.
  • The values corresponding to keys are the count of total trials for that location.
Here is the sample script which you can refer to:
x_vec = [0 2 3 7 8 1]';
y_vec = [1 4 5 2 9 6]';
locations = ["01" "24" "35" "72" "89" "16"];
values = [0 0 0 0 0 0];
d = dictionary(locations,values);
for k = 1 : length(x_vec)
n_trials = 5;
idx=randperm(length(x_vec),6);
x = x_vec(idx(k));
y = y_vec(idx(k));
temp = string(x) + string(y);
if(d(temp) >= 5)
%here put the logic to reshuffle the current location if condition
%is not met.
else
d(temp) = d(temp)+1;
end
end
Please go through the following documentation for more details on dictionary:
  2 Comments
HumbleCoder
HumbleCoder on 1 Jun 2023
Hi DV, I really appreciate your response. I have been trying your suggestion for a few days and been getting an undefined function/variable error for dictionary. Today it hit me that the function may not be avalible in Matlab 2019a? Am I correct? If i am, do you have an alterative solution for accomplishing my question above? I am hesistant to upgrade my Matlab version.
Thank you again for your help.
Dyuman Joshi
Dyuman Joshi on 1 Jun 2023
Yes, dictionary is available from 2022b and thus you can not access it in 2019a.
You can simply use logical indexing, paired with strcmp in case of the above example posted by Divyanshu.
Also replace
idx=randperm(length(x_vec),6)
with
idx = randi(length(x_vec))
and directly use idx.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!