Re: Balancing arrays matlab command
- From: ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford)
- Date: Wed, 01 Aug 2007 08:29:13 -0700
In article <f8q0d8$g2d$1@xxxxxxxxxxxxxxxxxx>, "Mark "
<medwar19.nospam@xxxxxxxxxxx> wrote:
Hi Roger,boxes are on a scale. Both boxes must be full (the same number of eggs)
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
after we are finished loading and swopping eggs takes time so swapping
should be minimized.
command for generating that matrix. I could then sort the matrix and see
I started with the n^2 solution and thought there might be a matlab
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
.
- Follow-Ups:
- Re: Balancing arrays matlab command
- From: Mark
- Re: Balancing arrays matlab command
- References:
- Re: Balancing arrays matlab command
- From: Roger Stafford
- Re: Balancing arrays matlab command
- From: Mark
- Re: Balancing arrays matlab command
- Prev by Date: Re: open more than one file
- Next by Date: Re: mex old f77 code
- Previous by thread: Re: Balancing arrays matlab command
- Next by thread: Re: Balancing arrays matlab command
- Index(es):
Relevant Pages
|