Home > other >  Selecting a random value from vector but excluding particular values
Selecting a random value from vector but excluding particular values

Time:09-17

I thought this was an easy one but I cannot find any solution for it.

I have this vector called cues= ["R" "B" "C" "P" "Y" "G"]; from which I want to randomly select one value, but excluding one (or two) of the values each time.

For example, I would like to get a random value from the vector excluding the "R" value, or in a second condition I would like "R" and "Y" not to be selected from the sample.

I have tried using randsample and randperm but neither of them seems to include this option.

CodePudding user response:

One intuitive way to achieve this would be taking your lists of all values and exclusions, and making a list of inclusions instead, then you can select from that list.

See the comments for each step:

cues= ["R" "B" "C" "P" "Y" "G"]; % All options
exclude = ["R" "Y"];             % Exclusions (could change in a loop or whatever)
include = setdiff( cues, exclude ); % Actual options without exclusions
selection = include( randi(numel(include)) ); % Random selection from options

CodePudding user response:

It seems I found a way to do it.

For a string vector, it can be done using erase function. For example:

cues_minusR = erase (cues,'R');

For a number vector (e.g., cues = [1 2 3 4 5 6]), it works this way:

c1Col=1;
c2Col=2;
cues(c1Col, : ) = [];
cues([c1Col, c2Col], : ) = [];
  • Related