How to generate matrix based on previous elements?

5 次查看(过去 30 天)
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".
  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.
  2. 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.
  3. 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.
  4. 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!!

采纳的回答

Torsten
Torsten 2022-10-15
编辑:Torsten 2022-10-15
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];
maxj = find(A(1,:)==1);
for i = 1:size(A,1)
j = find(A(i,:)==1);
maxj = max(j + 3,maxj + 3);
B(i,1:j-1) = 0;
B(i,j:maxj) = 1;
end
B
B = 4×14
0 1 1 1 1 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 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
  26 个评论
Torsten
Torsten 2022-10-31
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 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 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0];
I = 1;
while sum(A(I,:))==0
B(I,1) = 0;
I = I + 1;
end
maxj = find(A(I,:)==1);
for i = I:size(A,1)
if sum(A(i,:))==0
B(i,1) = 0;
continue
end
j = find(A(i,:)==1);
maxj = max(j + 3,maxj + 3);
if maxj > size(A,2)
break
end
B(i,1:j-1) = 0;
B(i,j:maxj) = 1;
end
B
B = 5×14
0 1 1 1 1 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 0 0 0 0 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 1 1 1 1 1 1 1 1 1

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2022-10-15
编辑:dpb 2022-10-16
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 个评论
dpb
dpb 2022-10-16
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by