how to randomly select an element from an array having the least probability in matlab

P
panilkumariitb · Apr 16, 2020 · 1.9K views
Question
how to randomly select an element from an array having the least probability in matlab example : A = [ 1,2,3,4,5,6] P = [ 0.01,0.2,0.25.0.2,0.25,0.09] % probability %
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Sep 16, 2026

To randomly select an element from an array that has the least probability in MATLAB, find all indices corresponding to the minimum probability using min_indices = find(P == min(P)) and randomly pick one index using randi(). If your goal is to sample across the entire array such that elements with lower probabilities have a higher chance of being drawn, use inverse-probability weighted sampling with datasample(A, 1, 'Weights', 1./P).

Method 1: Random Selection Among the Minimum-Probability Elements (Recommended)

Use this approach when you want to isolate elements that have the absolute lowest probability score and randomly break ties if multiple elements share that same minimum value:

% Define data array and corresponding probabilities
elements = {'Sensor_A', 'Sensor_B', 'Sensor_C', 'Sensor_D', 'Sensor_E'};
probabilities = [0.35, 0.05, 0.40, 0.05, 0.15];

% Step 1: Find the minimum probability value
min_val = min(probabilities);

% Step 2: Locate all indices that match the minimum probability
min_indices = find(probabilities == min_val); % Returns [2, 4]

% Step 3: Randomly pick one index from the candidate pool
random_choice_idx = min_indices(randi(numel(min_indices)));

% Step 4: Extract the selected element
selected_element = elements{random_choice_idx};

% Display output
fprintf('Minimum Probability: %.2f\n', min_val);
fprintf('Candidates with least probability: %s\n', strjoin(elements(min_indices), ', '));
fprintf('Randomly selected element: %s (Index: %d)\n', selected_element, random_choice_idx);

Method 2: Inverse Probability Weighted Sampling (Continuous Distribution)

If you want all elements to remain selectable, but with selection likelihoods inversely proportional to their original probability distribution (so elements with smaller \(P\) are chosen more frequently), use inverse weighting with datasample():

% Data elements and original probability distribution
A = [100, 200, 300, 400, 500];
P = [0.50, 0.25, 0.15, 0.08, 0.02]; % 500 has the least probability (0.02)

% Step 1: Calculate inverse weights (avoid division by zero with eps)
weights = 1 ./ (P + eps);

% Step 2: Normalize inverse weights into probability mass function
weights = weights / sum(weights);

% Step 3: Sample 1 element (or N elements) using Statistics Toolbox
selected_item = datasample(A, 1, 'Weights', weights);
fprintf('Sampled Element with Inverse Probability: %d\n', selected_item);

% Monte Carlo Verification (Draw 10,000 samples to verify distribution)
samples = datasample(A, 10000, 'Weights', weights);
tabulate(samples);

Method 3: Base MATLAB Implementation (No Toolboxes Required)

If you do not have the Statistics and Machine Learning Toolbox installed, implement the inverse sampling using the Cumulative Distribution Function (CDF) and rand():

A = [10, 20, 30, 40];
P = [0.4, 0.3, 0.2, 0.1];

% Compute normalized inverse weights
w = (1 ./ P) / sum(1 ./ P);

% Generate cumulative sum distribution
cdf_weights = cumsum(w);

% Draw uniform random value between 0 and 1
r = rand();

% Find first bin where cumulative sum exceeds random draw
selected_idx = find(r <= cdf_weights, 1, 'first');
result = A(selected_idx);

fprintf('Drawn value: %d\n', result);

Comparison of Approaches

Use Case MATLAB Approach Behavior
Strict Minimum Tie-Breaking min_idx = find(P == min(P));
val = A(min_idx(randi(end)));
Only elements sharing the absolute lowest probability are candidates; all higher probability elements are excluded.
Inverse Softmax / Weighted Sampling datasample(A, 1, 'Weights', 1./P) All elements have a chance of being selected, but lower original probabilities receive proportionately higher selection rates.

Key Functions Reference

  • min(): Finds the smallest value in the probability array.
  • find(): Returns the index positions of all elements matching the minimum condition.
  • randi(N): Generates a pseudo-random integer between \(1\) and \(N\) for uniform tie-breaking.
  • datasample(..., 'Weights', w): Performs weighted random sampling with replacement according to arbitrary weight vectors.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!