Re: Balancing arrays matlab command
- From: "Mark " <medwar19.nospam@xxxxxxxxxxx>
- Date: Wed, 1 Aug 2007 13:04:40 +0000 (UTC)
ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford) wrote in message <ellieandrogerxyzzy-3107071716360001@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
In article <f8o4a0$mdp$1@xxxxxxxxxxxxxxxxxx>, "Mark "
<medwar19.nospam@xxxxxxxxxxx> wrote:
Hi,Once both boxes are full I want to find which eggs to swap to acheive the
I have 2 egg boxes. I note the weight of each egg as I place it a box.
smallest difference in mass between boxes.
---------------------
Are there Matlab commands that would help solve this problem?
Thanks.
Mark.
It isn't clear to me how finding the smallest individual egg mass
difference between boxes will directly solve the problem of "balancing"
the two boxes. The best swapping might conceivably come from a number of
larger differences.
However, as to finding the smallest difference, you could always compare
every possible pairing between eggs in the two boxes, which would be an
order n^2 algorithm. If the numbers of your eggs are very large, you
might want a faster algorithm method.
One way is to do a sort on the combined egg weights and only compare
cases where an egg in one box is immediately followed weight-wise by an
egg of the other box. This constitutes an algorithm of order n*log(n)
instead. Let x be a row array of egg weights from the first box and y a
row array of egg weights in the second box. Do the following:
m = size(x,2); n = size(y,2);
[t,p] = sort([x,y]); % Sort the combined weights
q = 1:m+n; q(p) = q; % Get the inverse of p
u = zeros(1,m+n); % Put 0's where y-weights are located and
u(q(1:m)) = 1; % 1's where x-weights are located in t
f = find(diff(u)==-1); % Find where x-weights are followed by y-weights
ix1 = p(f); iy1 = p(f+1)-m; % Get the corresponding indices in x and y
g = find(diff(u)==1); % Find where y-weights are followed by x-weights
ix2 = p(g+1); iy2 = p(g)-m; % Get corr. indices in x and y
The ix1 and iy1 arrays are indices of pairs of x and y weights in which
the x-weight immediately precedes the y-weight in order of the combined
ascending weights. With ix2 and iy2, it is the same except that the
y-weight immediately precedes the x-weight in size.
If it is minimum absolute weight difference you seek, you could find it with
min([y(iy1)-x(ix1),x(ix2)-y(iy2)]).
On the other hand if you are after, say, the smallest positive weight
difference between the y-weights and the x-weights - that is, the y-weight
is the larger - you would want
min(y(iy1)-x(ix1)).
It all depends on what you want.
Roger Stafford
Hi Roger,
Thanks for that detailed thought process.
You were correct in your second case.
I aim is to balance full egg boxes within a tolerance.
The weight of each egg is only known after it is placed in a box (as the boxes are on a scale. Both boxes must be full (the same number of eggs) after we are finished loading and swopping eggs takes time so swapping should be minimized.
I started with the n^2 solution and thought there might be a matlab command for generating that matrix. I could then sort the matrix and see if any swap would take me within the tolerance.
It's a nice brain teaser!
Thanks,
Mark.
.
- Follow-Ups:
- Re: Balancing arrays matlab command
- From: Roger Stafford
- Re: Balancing arrays matlab command
- References:
- Re: Balancing arrays matlab command
- From: Roger Stafford
- Re: Balancing arrays matlab command
- Prev by Date: Set UserData
- Next by Date: creating fints with excel data
- Previous by thread: Re: Balancing arrays matlab command
- Next by thread: Re: Balancing arrays matlab command
- Index(es):
Relevant Pages
|