フィルターのクリア

index of a sequence

4 ビュー (過去 30 日間)
sami
sami 2013 年 5 月 15 日
コメント済み: Hajar Sadki 2021 年 2 月 23 日
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))]';
  1 件のコメント
sami
sami 2013 年 5 月 15 日
thank you too man

サインインしてコメントする。


sami
sami 2013 年 5 月 15 日
thank you man, it was very very helpful

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by