Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: minimise range of vector
Date: Sat, 31 Jan 2009 02:53:02 +0000 (UTC)
Organization: Loughborough University
Lines: 27
Message-ID: <gm0eee$imn$1@fred.mathworks.com>
References: <gm08oh$16q$1@fred.mathworks.com> <gm0dei$f2s$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1233370382 19159 172.30.248.38 (31 Jan 2009 02:53:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 31 Jan 2009 02:53:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1338633
Xref: news.mathworks.com comp.soft-sys.matlab:515087

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gm0dei$f2s$1@fred.mathworks.com>...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message <gm08oh$16q$1@fred.mathworks.com>...
> > Hi, I am trying to do something quite simple but am unsure as to the best way to go about it so was after some advice.
> > 
> > I have a vector, a=[a1 a2 a3 a4].
> > The sum(a) must = 10 and each member of a must be an integer. 
> > 
> > What's the best way to find a1:a4 such that the range of a is minimised?
> > 
> > Could I use optimisation techniques for this, or is there some more straightforward way? Thanks for reading.
> 
>   By 'range', do you mean the difference between the smallest and largest element?  Do you allow repetitions in the elements?  If the answers are both yes, the problem becomes trivial.  If the number of elements is a divisor of the required sum, the range is zero, otherwise it is one.  Perhaps you mean something else?
> 
>   In your case
> 
>  a = [3,3,4,4];
> 
> has a range of one because 4 is not a divisor of 10.
> 
> Roger Stafford


Yes, by range I mean the difference between the smallest and largest elements. And yes repetitions are allowed. The sum of the elements must in this case equal 10, and each element must be an integer. 

The reason I am minimising the range is because a vector of elements e.g. [3,3,2,2] is preferred over [4,4,1,1]. For the first vector the range is 1, and the second the range is 3. 

How can I code up the finding of these elements for an arbitrary length vector and and arbitrary value that they must sum to, subject to the integer constraint? Thanks.