How to generate matrix based on previous elements?
Show older comments
Hello all!
i will explain my problem , and i hope that you could help me because it's so critical.
i Have a matrix containing the arrival task (rows are tasks , columns are units of time slot), and each task take 3 units of time in processing.
A= [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0];
i said that each task take 3 units of time so,i put this in other matrix B, the time waiting and in the processing indicated by "1".
- task 1 arrives in the second time slot(second column), will pass 3 sec in processing so will quit in time slot number 5.
- task 2 arrives in the third time slot , should wait 2 units for being executed and 3 units in processing, total 5 units of time.
- task 3 arrives in the third time slot also, should wait 5 units (because it should wait the first for 2 units, the second for 3 units of procesing) and 3 for its processing so the total =8 units of time.
- task for arrives in the 6th time slot,should wait task 2 for 2 units , task 3 for 3 units,and , the total = 8 units of time.
the matrix B is the result that i want: the "1" indicates the time explained above.
B= [0 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0];
Please help me how to generate B?
HELP PLEASE!!
Accepted Answer
More Answers (1)
dt=3; % use variables for data; don't bury magic numbers in code
[r,arr]=find(A); % get the arrival time each row
S=zeros(size(r)); E=S: % initialize start, end indices for each
S(1)=arr(1);
E(1)=arr(1)+dt;
for i=2:numel(r)
S(i)=max(E(i-1)+1,arr(i)); % next start is previous end+1 or arrival
E(i)=S(i)+dt;
end
13 Comments
Safia
on 15 Oct 2022
What do you mean? The array B above is identical to what your posting says is the desired result...
B_posted=[0 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0];
B_dpb =[ 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 ];
all(B_posted==B_dpb)
shows it agrees for all four rows for all columns. What more can you ask?
The sum by column is
disp([sum(B_dpb,2) sum(B_posted,2)])
Safia
on 15 Oct 2022
dpb
on 15 Oct 2022
You're going to have to have a different set of rules, then; and provide the other data in order for anybody to have even half a chance.
The above code follows the prescribed rules for any data set; if you change the rules, then all bets are off.
Safia
on 15 Oct 2022
dpb
on 15 Oct 2022
I don't believe it...gonna' have to show me, I'm a "doubting Thomas" without something concrete to look at.
Post your code and the data run against it in a runnable example.
dpb
on 16 Oct 2022
It doesn't need to be big to illustrate a condition you think my solution doesn't work for...and you haven't shown the code you used...my certainly will have operated over the full number of rows.
If there were some condition other than considered/not represented in the dataset have overlooked, then show us that condition.
It can happen in the first few rows and columns; just because you have a large real case doesn't matter; a 10x10 is surely big enough to represent any condition that might arise.
Safia
on 16 Oct 2022
See this example for A. I think it's not treated correctly.
A= [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0];
dt=3; % use variables for data; don't bury magic numbers in code
B=A; % initialize output array
[r,arr]=find(A); % get the arrival time each row
E=arr(1)+dt; % initial end time
B(1,arr(1):E)=1;
for i=2:numel(r)
E=E+dt
B(i,arr(i):E)=1;
end
B
dpb
on 16 Oct 2022
I see I ended up pasting the wrong code in; it was a first pass I pulled from the commandhistory instead of the later -- it did produce the correct answer for the sample but didn't correctly consider the disparate distances, but then I just looked at the result instead of the code not realizing what had done...
I updated the Answer to reflect; I think this does what you want for the S (start) and E (end) values; it presumes the next START cycle can't be until one AFTER the previous end; if your counting is that the next START can be the same increment as the previous end, remove the "+1" adjustment...then if you're still using the large array of ones and not just the much-more-storage-efficient indices, then fill it between S:E for each row.
Safia
on 16 Oct 2022
dpb
on 16 Oct 2022
What you mean? Have all start/end values; just stuff those into an array if still using.
dt=3; % use variables for data; don't bury magic numbers in code
[r,arr]=find(A); % get the arrival time each row
S=zeros(size(r)); E=S: % initialize start, end indices for each
S(1)=arr(1);
E(1)=arr(1)+dt;
B(1:S(1):E(1))=1;
for i=2:numel(r)
S(i)=max(E(i-1)+1,arr(i)); % next start is previous end+1 or arrival
E(i)=S(i)+dt;
B(i:S(1):E(1))=1;
end
Categories
Find more on Get Started with MATLAB 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!