Re: Balancing arrays matlab command



In article <f8q0d8$g2d$1@xxxxxxxxxxxxxxxxxx>, "Mark "
<medwar19.nospam@xxxxxxxxxxx> wrote:

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.
---------------------
I should have read your original article more carefully, Mark. In my
haste, I misinterpreted the phrase, "which eggs to swap to acheive the
smallest difference in mass between boxes" to mean "between EGGS!", which
is what the code I described accomplishes. So toss out that code and
begin again.

For your actual problem, let x and y again be row vectors of the two
sets of egg weights. Let w be the total weight difference between box x
and box y which you are trying to best adjust with a single swap. Do
this:

D = repmat(x,size(y,2),1)-repmat(y',1,size(x,2)); % All x-y differences
[c,lx] = min(abs(D(:)-w/2)); % Get the closest match with w/2
[iy,ix] = ind2sub(size(D),lx); % Get the corresponding indices

c will be the smallest discrepancy in box weights attainable with a single
swap, and (ix,iy) will be the respective x and y indices for the
corresponding eggs to be swapped.

If you contemplate more than one swap to achieve the needed correction,
the problem becomes more difficult. For example, to solve the two-swap
problem might require finding the differences between every possible pair
of x-box eggs with every possible pair of y-box eggs, an n^4 size matrix
of differences. If your boxes contain only, say, a dozen eggs each, this
approach would not be out of the question, however. Triple or quadruple
swaps would probably begin to require more sophisticated algorithms. Let
us know if you seriously envision going beyond single swaps.

Roger Stafford
.



Relevant Pages

  • Re: Balancing arrays matlab command
    ... I added your lines to my eggs problem to see what happens. ... boxes are full a single swap usually get sufficent balance. ... to swap a second time if needed and that was always successful. ... % Still not in tolerance after swap so force another go ...
    (comp.soft-sys.matlab)
  • Re: Building coop
    ... Because they then poop in it - and you get dirty eggs. ... Roosting bars should be away from the nest boxes ... Many young birds that have been reared in barns have not met a roost bar ...
    (sci.agriculture.poultry)
  • Re: Self-checkout messing up LOL
    ... had a supermarket coupon for cheap eggs, limit 2 boxes. ... Duly rang up all ...
    (rec.food.cooking)
  • Re: Self-checkout messing up LOL
    ... had a supermarket coupon for cheap eggs, limit 2 boxes. ... Duly rang up all ...
    (rec.food.cooking)
  • Re: Self-checkout messing up LOL
    ... had a supermarket coupon for cheap eggs, limit 2 boxes. ... Duly rang up all ...
    (rec.food.cooking)