take a sum in a matrix and print it on a different row

4 views (last 30 days)
process time start time end time
2 0
1
3
6
Suppose I have such a matrix.
I want to make start time + process time and find the end time. For example, the end time of the first row is 2.
then i want to do this:
The start time of the second row = the end time of the first time
End time of second row = end time of first row+process time of second row
For example :
process time start time end time
2 0 2
1 2 3
3 6 9
6 9 15
How can I do this for a large matrix?
Thank u for help

Accepted Answer

Matt J
Matt J on 29 Mar 2022
Edited: Matt J on 29 Mar 2022
proctime=[2 1 3 6];
n=numel(proctime);
endtime=cumsum(proctime);
starttime=[0,endtime(1:n-1)];
table(proctime',starttime',endtime','Var',{'Process Time','Start Time', 'End Time'})
ans = 4×3 table
Process Time Start Time End Time ____________ __________ ________ 2 0 2 1 2 3 3 3 6 6 6 12

More Answers (2)

Torsten
Torsten on 29 Mar 2022
A = process time vector (as a column vector)
B = zeros(numel(A),3);
B(:,1) = A(:,1);
C = cumsum(B(:,1));
B(2:end,2) = C(1:end-1);
B(:,3) = C(:);

Voss
Voss on 29 Mar 2022
If you are starting with an Nx3 matrix of (process, start, end) times:
% initial matrix:
% process start end
times = [2 0 NaN; ... % doesn't matter what the elements where I have NaNs are initially.
1 NaN NaN; ... % they will be overwritten.
3 NaN NaN; ...
6 NaN NaN];
% fill in the remaining start times:
times(2:end,2) = times(1,2)+cumsum(times(1:end-1,1))
times = 4×3
2 1 NaN 1 3 NaN 3 4 NaN 6 7 NaN
% fill in the end times:
% end time = start time + process time
times(:,3) = times(:,2)+times(:,1);
disp(times);
2 1 3 1 3 4 3 4 7 6 7 13
Or if you are starting with a single initial start time and a vector of process times:
% initial start time:
initial_start_time = 0;
% vector of process times:
process_time = [2; 1; 3; 6];
% calculate the end times:
end_time = initial_start_time+cumsum(process_time);
% the (i+1)th process starts when the ith process ends:
start_time = [initial_start_time; end_time(1:end-1)];
% put everything together in a matrix:
times = [process_time start_time end_time];
disp(times);
2 1 3 1 3 4 3 4 7 6 7 13

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!