index of a sequence

4 views (last 30 days)
sami
sami on 15 May 2013
Commented: Hajar Sadki on 23 Feb 2021
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-------------------

Accepted Answer

Matt Kindig
Matt Kindig on 15 May 2013
Edited: Matt Kindig on 15 May 2013
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 Comment
Hajar Sadki
Hajar Sadki on 23 Feb 2021
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 ?

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 15 May 2013
[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 on 15 May 2013
thank you man, it was very very helpful

Community Treasure Hunt

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

Start Hunting!