take a sum in a matrix and print it on a different row
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
More Answers (2)
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
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))
% fill in the end times:
% end time = start time + process time
times(:,3) = times(:,2)+times(:,1);
disp(times);
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);
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!