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 (한국어)