Clear Filters
Clear Filters

For loop

1 view (last 30 days)
Ayda
Ayda on 21 Apr 2012
Good evening\morning i want to get the same result of the ascending method from the for loop.
I have to use the for loop because i want to calculate the time for each job.
numOfJobs=5;
for i=1:numOfJobs
job(i)=i;
end
arrivalTime=[2 4 1 5 3];
[sortedArrivalTimes, job] = sort(arrivalTime);
%*****************************************
numOfJobs=5;
for i=1:numOfJobs
job(i)=i;
end
arrivalTime=[2 4 1 5 3];
for i=1:numOfJobs
y=i+1;
for j=1:numOfJobs-1
if(arrivalTime(j)<arrivalTime(i))
firstcome=job(j);
job(j)=job(i);
job(i)=firstcome;
end
end
end
job
  1 Comment
Image Analyst
Image Analyst on 21 Apr 2012
Here's another way to sort: http://blogs.mathworks.com/steve/2012/04/11/and-now-for-something-completely-different/

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 21 Apr 2012
Looks like you're failing to swap arrival times in your loop.
  1 Comment
Ayda
Ayda on 21 Apr 2012
is it wrong if i swap !

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!