I got a task From my group studying class the Question is Quite confusing and I'm New to matlab. Here Is my Question and Plz Help me guys.
1 view (last 30 days)
Show older comments
Write a Matlab script that, given the capacity of each room in the building and the size of a class, will attempt to find satisfactory room assignment that will accommodate the class in the building. If the class cannot be satisfactory placed, the script should print a “Room is not available” message, otherwise, a message saying that “Room number XX which has YY seats, is best for the class of size ZZ”.
An array indexed from 1 to 10 should be convenient structure for representing the capacities of each room in the building. The indices of the array element represent the room number as pictured below
To find a satisfactory room assignment (Best fit room number for a given class size), the number of leftover –remaining- seats for the room when the assignment is made should be minimum, where the (leftover seats) = (room capacity) - (class size).
4 Comments
Answers (3)
Image Analyst
on 2 Dec 2016
Here's a start. I trust you can finish it:
numClasses = 10;
roomCapacities = sort(randi(200, 1, numClasses), 'descend') % Sample data.
classSizes = sort(randi(200, 1, numClasses), 'descend')
classroomOccupied = false(1, numClasses)
for k = 1 : numClasses
for c = 1 : numClasses
if classSizes(k) < roomCapacities(c) && ~classroomOccupied(c)
% Code for you to finish....
end
end
end
This is only one way of many, I'm sure.
4 Comments
Image Analyst
on 3 Dec 2016
Where in the last 3 lines of the code I posted for you do you see a loop? Here they are again:
emptySeats = roomCapacities - classSize;
emptySeats(emptySeats < 0) = inf;
[minValue, index] = min(emptySeats);
There is no loop there. Which line has a "for" or "while" word on it?
Andrei Bobrov
on 3 Dec 2016
r = [12 23 44 54 23 6 65 100 212 45]
c = [70,6,23,200]'
[a,b] = sort(r(:))
[ii,~] = find(cumsum((a(:) - c(:)') >= 0) == 1)
out = [b(ii),a(ii),c(:)]
Abu Yamen
on 4 Dec 2016
we should not use loop in this q only we can use find function and size then fprintf
1 Comment
Walter Roberson
on 4 Dec 2016
It says that you cannot use loops but where does it say that you must use find()? There is a perfectly good solution with sort and interp1
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!