index of a sequence

4 次查看(过去 30 天)
sami
sami 2013-5-15
Hello Everyone,
i have a small problem regarding Johnson's Algorithm, Scheduling (2 machines(a,b), 5 jobs(5 columns for each )
%a=[50 150 80 200 30];
%b=[60 50 150 70 200];
my idea is to find the min(min(a),min(b)) (application : min(30,50) = 30) in a FOR LOOP (i:5) then construct a sequence of the two machines (a,b) , regarding each side of the machines (a,b) , for example if the first min is in (a) then put it LEFT otherwise put it RIGHT after that SORT each side separately , the final result i get is : 30 50 80 70 50 , what i want to get is the INDEX of this sequence, for example the index of this sequence "30 50 80 70 50" should be "5 1 3 4 2" .
%-------------the code-------------------
clear all;clc
a=[50 150 80 200 30];
b=[60 50 150 70 200];
comp01=[];
comp02=[];
for i = 1:5
t= min((a(i)),(b(i)));
if a(i)== t
comp01 = [t,comp01];
left = comp01;
left = sort(left,'ascend');
%[left,x1] = sort(left);
else
comp02 = [t,comp02];
right = comp02;
right = sort(right,'descend');
%[right,x2] = sort(right);
end
end
Sequence_in_numbers = [left right]
%Sequence_in_index = [x1 x2]
%-------------end of the code-------------------

采纳的回答

Matt Kindig
Matt Kindig 2013-5-15
编辑:Matt Kindig 2013-5-15
You don't really need the loops:
a=[50 150 80 200 30];
b=[60 50 150 70 200];
[d,rows]= min([a;b], [], 1); %get lowest value in each column
lcols = find(rows==1); % columns where a is smaller
rcols = find(rows==2); % columns where b is smaller
[left,lorder] = sort(d(lcols), 'ascend'); %left elements
[right,rorder] = sort(d(rcols),'descend'); %right elements
Sequence_in_numbers = [left right]; %sequence of numbers
Sequence_in_index = [lcols(lorder), rcols(rorder)]; %index of the sequence
  1 个评论
Hajar Sadki
Hajar Sadki 2021-2-23
i have two machines Ai Bi and i=1...n . i'm looking for sequence of numbers and index of the sequence. it is same code ? what can i change ?

请先登录,再进行评论。

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2013-5-15
[v,ii]= min([a;b]);
t = accumarray(ii',(1:numel(a))',[],@(x){x});
[v2,i2]=cellfun(@(x)sort(v(x)),t,'un',0);
sn = [v2{1},v2{2}(end:-1:1)];
si = [t{1}(i2{1});t{2}(i2{2}(end:-1:1))]';

sami
sami 2013-5-15
thank you man, it was very very helpful

类别

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