이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to generate matrix based on previous elements?
조회 수: 3 (최근 30일)
이전 댓글 표시
Safia
2022년 10월 15일
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!!
채택된 답변
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
Safia
2022년 10월 15일
@Torsten in each row , i have only "1". this is a part of my matrix A :
0 0 0 1 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
this is the result after using the code above:
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
and i verify with the sum of each row, i get :
4
4
47
6
4
4
4
18
i hope that you inderstand
Torsten
2022년 10월 15일
i hope that you inderstand
No, I don't.
You didn't mention that you have zero rows (like the second and the fourth).
Should they be skipped in the process of building B ?
Torsten
2022년 10월 16일
편집: Torsten
2022년 10월 16일
- Each row must contain exactly one 1.
- The column numbers in which the 1's appear are monotonically increasing, i.e. a case like A = [0 1 0; 1 0 0; 0 0 1] cannot be handled.
If these requirements are met, the code
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
should work. If not, you will have to show a matrix A for which it doesn't work as expected.
Safia
2022년 10월 16일
편집: Safia
2022년 10월 16일
It works only with some rows,it seems that the problem in my matrix because I will focus on time (columns)of arriving task(rows),for example
A= [0 0 1 0 0 0 0
0 1. 0. 0. 0. 0. 0
1. 0. 0. 0. 0. 0. 0]
In this case, task 3 will be executed first because it is arriving first
Task 2 will be executed after 2 units
Task 1 is arriving last so it will wait the First for 1 second and task 2 for 4 units.(total 5) Matrix B will be like this:
B= [0 0 1 1 1 1 1 1
0 1. 1. 1. 1. 1. 1 0
1. 1. 1. 1. 0. 0. 0. 0]
I can't represent all elements
dpb
2022년 10월 16일
편집: dpb
2022년 10월 16일
I hadn't seen this subthread ere now, but you keep adding stuff that wasn't in the original posting.
As @Torsten says, remove null rows; there can't be any point in keeping them it would seem...if, for some reason it is needed to know there are such, then
idxZ=find(~any(A,2));
will be the rows that were null in the original; numel(idxA) will be how many there were for bookkeeping
You then introduced the randomized first starting position that is a new wrinkle not in the original problem specification.
[A,idxA]=sortrows(A,[1:size(A,2)],'descend');
first; then use either mine or Torsten's code.
If needed, have the idxA array to produce the original order.
Or, you could process in the loop by stepping through with the idxA order instead of sequentially; just adds a bunch of additonal indirect addressing subscripting to the code; will leave that as "exercise for student" to actually do if want to go that way.
Safia
2022년 10월 16일
Torsten
2022년 10월 28일
편집: Torsten
2022년 10월 28일
Like this ?
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];
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);
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
2022년 10월 31일
편집: Safia
2022년 10월 31일
@Torsten hi! i have a question if you don't mind ,how cant i put i condition to not exceed array bounds. I want to stop in the limit of my matrix. with your provided code, the number of columns increases over 100 iterations
I will be grateful if you could help me.Thnak you so much
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
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
2022년 10월 15일
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)
ans = 1×15 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
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)])
4 4
6 6
9 9
9 9
dpb
2022년 10월 15일
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.
dpb
2022년 10월 15일
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
2022년 10월 16일
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
2022년 10월 16일
@dpb i don't have a condition expect, if the arrival the difference of time units between the arrival task.
for example , here "arr" display the time of arrival task :
arr =
54
56
60
81
160
182
191
214
240
263
there is a large difference between them, but as we see the case of the second arriving in time slot 56 should wait the first arriving in 54 for a unit ,because it doesn't finish yet (as i said each task take 3units of time). Did you understand the case?
Torsten
2022년 10월 16일
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
E = 8
E = 11
E = 14
B
B = 4×15
0 1 1 1 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 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
dpb
2022년 10월 16일
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.
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 Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)