- minimize the number of boxes
- maximum box area/side-length/...
- minimum box area/side-length/...
- range/mean/mode/standard distribution of box size
- some weighted function of several metrics (if so, what metrics and what weights).
Merging uniform boxes into larger ones
2 Comments
Accepted Answer
2 Comments
More Answers (2)
Hi @Pete sherer,
To enhance your existing code for merging boxes more efficiently, you can consider a more sophisticated approach. Below is an updated version of your code with an added heuristic method that attempts to minimize the number of resulting boxes while preserving their uniformity.
function [boxNewMask] = optimizeBoxMerging(boxGrid) [rows, cols] = size(boxGrid); visited = false(rows, cols); % Track visited cells boxNewMask = zeros(rows, cols); % Initialize new box mask grpLabel = 0; % Group label initialization
% Iterate through each cell in the grid for iRow = 1:rows for jCol = 1:cols if boxGrid(iRow, jCol) == 1 && ~visited(iRow, jCol) grpLabel = grpLabel + 1; % Merge boxes using a breadth-first search (BFS) approach [height, width] = mergeBoxes(boxGrid, visited, iRow, jCol);
% Mark the merged area as visited idxRow = iRow:iRow+height-1; idxCol = jCol:jCol+width-1; boxNewMask(idxRow, idxCol) = grpLabel; visited(idxRow, idxCol) = true; end end end end
function [height, width] = mergeBoxes(boxGrid, visited, startRow, startCol) [rows, cols] = size(boxGrid);
% Initialize dimensions height = 0; width = 0;
% Find maximum width for col = startCol:cols if boxGrid(startRow, col) == 1 && ~visited(startRow, col) width = width + 1; else break; end end
% Find maximum height while maintaining width for row = startRow:rows if all(boxGrid(row, startCol:startCol+width-1) == 1) && ... all(~visited(row, startCol:startCol+width-1)) height = height + 1; else break; end end
% Optional improvement: Attempt to expand horizontally if possible for extraWidth = 1:min(width, cols - (startCol + width)) if all(boxGrid(startRow:startRow+height-1, startCol + extraWidth) == 1) width = width + 1; % Expand width if possible else break; end end end
% Example usage: boxGrid = [1 1 0 0 1; 1 1 0 1 1; 0 0 0 1 1; 1 0 0 0 0]; % Your original grid here. boxNewMask = optimizeBoxMerging(boxGrid); disp(boxNewMask);
Please see attached.
A new function mergeBoxes is created to find and merge contiguous boxes more effectively. This function includes logic to expand horizontally where possible. The merging process is structured to resemble Breadth First Search (BFS). This method allows exploring all potential expansions of a box before marking it as merged. As mentioned in the comments you received, considering heuristics or algorithms like genetic algorithms or simulated annealing could provide better solutions for larger datasets.
This updated implementation should give you a good starting point toward achieving your goal of minimizing the number of boxes while still accurately reflecting their positions in the grid.
If you have specific scenarios or datasets you'd like to test against this code further, please let me know!
0 Comments
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!