錯誤 `Attempted to access boxes(:,-1); index must be a positive integer or logical.` 發生在 `reduceboxes` 函數第 16 行 `maxc = max(boxes(:,end-1));`。這表明 `boxes` 矩陣為空或列數不足,導致 `end-1` 索引無效(變為 0 或負數)。
function rb = reduceboxes(model, boxes)
% 原始代碼開頭插入防禦
if isempty(boxes) || size(boxes, 2) < 2
rb = zeros(4,0); % 返回空框矩陣
return;
end
% ... 原有邏輯
maxc = max(boxes(:,end-1)); % 此處安全
end
若項目允許,優先考慮遷移到 **PyTorch/TensorFlow 版本的 Faster R-CNN、YOLOv5+** 等現代目標檢測框架,社區活躍、Windows 支持完善、文檔豐富,避免在 10 年前的 MATLAB DPM 代碼上耗費過多精力。