Passenger belt monitoring system

2 views (last 30 days)
I want to compute a program that is a monitor system that has the capability to count the front two passengers in each car and can detect who use the belt in each car. i should make a program that generate 20000 random numbers of cars of different number of passengers (one or two) also , I need to identify the number of passengers in each car, and how many one use the belt in each car (generated randomly, i.e. 0 for no belt and 1 for belt)

Accepted Answer

Image Analyst
Image Analyst on 27 Nov 2019
How about (untested):
numCars = 20000;
data = ones(numCars, 2); % Col 1 is always 1 because car must have a driver
% Now assign whether or not there is a passenger (50% probability)
data(:, 2) = randi([0, 1], numCars, 1);
numOccupants = sum(data, 2);
% Now say who is wearing a seat belt (50% chance for each to wear it).
beltUsage = randi([0, 1], numCars, 2);
Adjust as needed for different probabilities.

More Answers (1)

Image Analyst
Image Analyst on 27 Nov 2019
If you want more control over who's wearing seatbelts by specifying the probability of earing a seatbelt and the probability of having a passenger, see this code:
% Define parameters
numberOfCars = 20000;
probabilityOfAPassenger = 0.5;
probabilityOfDriverWearingSeatbelt = 0.8;
probabilityOfPassengerWearingSeatbelt = 0.9;
% Now make arrays for which seats are occupied.
seatOccupied = zeros(numberOfCars, 2); % Instantiate.
seatOccupied(:, 1) = 1; % Col 1 is always 1 because car must have a driver
% Now assign whether or not there is a passenger (with specified probability of there being a passenger.)
numPassengers = round(probabilityOfAPassenger * numberOfCars)
passengerIndexes = sort(randperm(numberOfCars, numPassengers))';
seatOccupied(passengerIndexes, 2) = 1;
% Now count how many people are in each car by summing across columns.
numOccupants = sum(seatOccupied, 2);
% See how many people are sitting in each seat location.
numSitting = sum(seatOccupied, 1)
% Now say who is wearing a seat belt (50% chance for each to wear it).
beltUsage = seatOccupied; % Initialize at first to everyone wearing a seatbelt if the seat is occupied.
r = rand(numberOfCars, 1);
% Compute which drivers are wearing seatbelts.
beltUsage(:, 1) = beltUsage(:, 1) .* (r <= probabilityOfDriverWearingSeatbelt);
% Compute which passengers are wearing seatbelts.
r = rand(numberOfCars, 1);
beltUsage(:, 2) = beltUsage(:, 2) .* (r < probabilityOfPassengerWearingSeatbelt);
% See how many are wearing belts in each seat location
numWearingSeatbelts = sum(beltUsage, 1)

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!