필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by