How to generate matrix based on previous elements?

7 Ansichten (letzte 30 Tage)
Safia
Safia am 15 Okt. 2022
Kommentiert: Safia am 1 Nov. 2022
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!!
  3 Kommentare
Safia
Safia am 15 Okt. 2022
@dpb i appreciate all their effort.
Jan
Jan am 15 Okt. 2022
@Safia: Avoid the term "urgent", because it is couter-productive. See here.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 15 Okt. 2022
Bearbeitet: Torsten am 15 Okt. 2022
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 Kommentare
Torsten
Torsten am 31 Okt. 2022
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
Safia
Safia am 1 Nov. 2022
Thank you so much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 15 Okt. 2022
Bearbeitet: dpb am 16 Okt. 2022
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 Kommentare
Safia
Safia am 16 Okt. 2022
How can I get the result no after update code ?
dpb
dpb am 16 Okt. 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

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by