MATLAB: How to find total duration

MATLAB

A = [5 9 3 4];; %number of days to complete a work
i have 2 set of workers,
start time = 0;
0 + 5 = 5
0 + 9 = 9
then i need to check which work gets completed first and then allocate that worker to the next work, so next work takes 8 days to complete
endtime_of_first_completed_work + next_work's_numberofdays
doing so i need to get,
[5 9 8 12]

Best Answer

I assumed these for my answer: n = 4; m = 1; the problem lies within [v, I] = min(st(1:j-1)); you shouldn't look for the minimal of the array. Instead you should look for the minimum between the 2 workers. I would suggest to use an array for the indices of the workers. Here, worker = zeros(1,2). The end program will look something like this:
n = 4;
m = 1;
worker = zeros(1,2);
st = zeros(1,4);
Dur = [5 9 3 4];
for i = 1 : m
for j = 1 : n
k = 2;
if j <= k
worker(find(st==0,1)) = j;
st(1,j) = st(1,j) + Dur(i,j);
else
[v, I] = min(st(worker));
st(j) = st(worker(I)) + Dur(j);
worker(I) = j;
end
end
end
Related Question