group seating algorithm in matlab

Here's an example algorithm for grouping seating in MATLAB:

main.m
% Define seat matrix with assigned values
seatValues = [3 3 3 3 2 2 2 2 1 1];
numSeats = length(seatValues);

% Define group sizes and number of groups
groupSizes = [3 2 2];
numGroups = length(groupSizes);

% Initialize variables
assignedSeats = zeros(1, numSeats); % records which seat is assigned to which group
groupSizesRemaining = groupSizes; % records how many spots remaining for each group
seatValuesRemaining = seatValues; % records which spots are still available

% Iterate through groups and assign seats
for i = 1:numGroups
    % For each group, find the biggest unassigned spots that fit the group size
    [sortedSeatValues, sortedSeatIndices] = sort(seatValuesRemaining, 'descend');
    idx = find(groupSizesRemaining(i) <= cumsum(sortedSeatValues), 1);
    spots = sortedSeatIndices(1:idx);
    
    % If there are no suitable spots left, break the loop
    if isempty(spots)
        break;
    end
    
    % Assign the spots to this group
    assignedSeats(spots) = i;
    groupSizesRemaining(i) = groupSizesRemaining(i) - length(spots);
    seatValuesRemaining(spots) = [];
end

% Print out the final assignments
fprintf('Assigned seats: ');
for i = 1:numSeats
    fprintf('%d ', assignedSeats(i));
end
fprintf('\n');
1238 chars
38 lines

This algorithm assigns seats to groups based on the size of the group and the value (preference) of the seats. It first sorts the available seats by value, and then iterates through each group and finds the biggest unassigned seats that will fit the group size. If there are no suitable seats left, the algorithm stops assigning seats to groups. The final assignments of seats are displayed at the end.

gistlibby LogSnag