Re: Balancing arrays matlab command



ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford) wrote in message <ellieandrogerxyzzy-3107071716360001@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
In article <f8o4a0$mdp$1@xxxxxxxxxxxxxxxxxx>, "Mark "
<medwar19.nospam@xxxxxxxxxxx> wrote:

Hi,

I have 2 egg boxes. I note the weight of each egg as I place it a box.
Once both boxes are full I want to find which eggs to swap to acheive the
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.

.



Relevant Pages

  • Re: Balancing arrays matlab command
    ... I have 2 egg boxes. ... I note the weight of each egg as I place it a box. ... One way is to do a sort on the combined egg weights and only compare ... row array of egg weights in the second box. ...
    (comp.soft-sys.matlab)
  • Re: Analysing text boxes on sub form/simplifying evet procedures
    ... <start of sermon> ... you want to find all weights in a particular range? ... I have a form and within that a subform that contains 10 text ... > I have some VB code that checks the contents of all ten boxes. ...
    (microsoft.public.access.formscoding)
  • Thieving Magpies!
    ... the lack of any eggs at all and today my suspicions were confirmed by ... surprising a magpie with a broken egg. ... loose box with the nest boxes being old wooden fruit boxes nailed to the ... how do I stop the magpies; ...
    (sci.agriculture.poultry)
  • Re: Balancing arrays matlab command
    ... Mark wrote: ... I have 2 egg boxes. ... I note the weight of each egg as I place it a ...
    (comp.soft-sys.matlab)
  • Balancing arrays matlab command
    ... I note the weight of each egg as I place it a box. ... Once both boxes are full I want to find which eggs to swap to acheive the smallest difference in mass between boxes. ... Are there Matlab commands that would help solve this problem? ...
    (comp.soft-sys.matlab)