Re: Balancing arrays matlab command
- From: ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford)
- Date: Wed, 01 Aug 2007 00:16:37 GMT
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
.
- Follow-Ups:
- Re: Balancing arrays matlab command
- From: Mark
- Re: Balancing arrays matlab command
- Prev by Date: Convert video to bitstream
- Next by Date: regrading wavelet
- Previous by thread: Convert video to bitstream
- Next by thread: Re: Balancing arrays matlab command
- Index(es):
Relevant Pages
|