Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
minimise range of vector

Subject: minimise range of vector

From: Dave Brackett

Date: 31 Jan, 2009 01:16:01

Message: 1 of 6

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.

Subject: minimise range of vector

From: Roger Stafford

Date: 31 Jan, 2009 02:36:02

Message: 2 of 6

"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

Subject: minimise range of vector

From: Dave Brackett

Date: 31 Jan, 2009 02:53:02

Message: 3 of 6

"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.

Subject: minimise range of vector

From: Roger Stafford

Date: 31 Jan, 2009 03:42:02

Message: 4 of 6

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <gm0eee$imn$1@fred.mathworks.com>...
> 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.

  Let n be the number of elements in the vector and s their sum.

 r = rem(s,n);
 q = (s-r)/n;
 v = fix(q+(r+sign(s)*(0:n-1))/n);

Then v is your desired vector.

  Note: this could be a little simpler if s were known to always be positive.

Roger Stafford

Subject: minimise range of vector

From: Roger Stafford

Date: 31 Jan, 2009 13:47:01

Message: 5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gm0haa$jal$1@fred.mathworks.com>...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message <gm0eee$imn$1@fred.mathworks.com>...
> > 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.
>
> Let n be the number of elements in the vector and s their sum.
>
> r = rem(s,n);
> q = (s-r)/n;
> v = fix(q+(r+sign(s)*(0:n-1))/n);
>
> Then v is your desired vector.
>
> Note: this could be a little simpler if s were known to always be positive.
>
> Roger Stafford

  I could just as well used the simpler one-liner:

 v = floor((s+(0:n-1))/n);

It works for all integers s, positive or negative, and all positive integers, n.

Roger Stafford

Subject: minimise range of vector

From: Dave Brackett

Date: 31 Jan, 2009 17:39:01

Message: 6 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gm1kol$fd6$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gm0haa$jal$1@fred.mathworks.com>...
> > "Dave Brackett" <davebrackett@hotmail.com> wrote in message <gm0eee$imn$1@fred.mathworks.com>...
> > > 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.
> >
> > Let n be the number of elements in the vector and s their sum.
> >
> > r = rem(s,n);
> > q = (s-r)/n;
> > v = fix(q+(r+sign(s)*(0:n-1))/n);
> >
> > Then v is your desired vector.
> >
> > Note: this could be a little simpler if s were known to always be positive.
> >
> > Roger Stafford
>
> I could just as well used the simpler one-liner:
>
> v = floor((s+(0:n-1))/n);
>
> It works for all integers s, positive or negative, and all positive integers, n.
>
> Roger Stafford

thanks a lot Roger, that works really well, and is a much simpler way than I was trying.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us