MATLAB: Index of a sequence

index of a sequence

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-------------------

Best Answer

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