필터 지우기
필터 지우기

How to generate matrix based on previous elements?

조회 수: 5 (최근 30일)
Safia
Safia 2022년 10월 15일
댓글: Safia 2022년 11월 1일
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
Safia
Safia 2022년 10월 15일
@dpb i appreciate all their effort.
Jan
Jan 2022년 10월 15일
@Safia: Avoid the term "urgent", because it is couter-productive. See here.

댓글을 달려면 로그인하십시오.

채택된 답변

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
Safia
Safia 2022년 11월 1일
Thank you so much

댓글을 달려면 로그인하십시오.

추가 답변 (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
Safia
Safia 2022년 10월 16일
How can I get the result no after update code ?
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