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:
Creating array with specific sum

Subject: Creating array with specific sum

From: Mohd

Date: 22 Jul, 2011 09:37:09

Message: 1 of 10

Hi,

I want to create a 1xn array, that come out with specific total. For example, given the summation of array is 20. Then, I want to create an array with 1x5 matrix. The answer might be as follow:
[5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.

Thank you.

Subject: Creating array with specific sum

From: Bruno Luong

Date: 22 Jul, 2011 11:02:10

Message: 2 of 10

"Mohd" wrote in message <j0bgc5$g05$1@newscl01ah.mathworks.com>...
> Hi,
>
> I want to create a 1xn array, that come out with specific total. For example, given the summation of array is 20. Then, I want to create an array with 1x5 matrix. The answer might be as follow:
> [5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.
>

The bellow will create exactly the array as your requirement, however I'm almost sure it's not what you really want.

>> a=rand(1,4)

a =

    0.8147 0.9058 0.1270 0.9134

>> a(end+1)=20-sum(a)

a =

    0.8147 0.9058 0.1270 0.9134 17.2391

Up to you to describe correctly the constraints, requirement that are still missing.

Bruno

Subject: Creating array with specific sum

From: Lothar Schmidt

Date: 22 Jul, 2011 11:07:59

Message: 3 of 10

Am 22.07.2011 11:37, schrieb Mohd:
> Hi,
>
> I want to create a 1xn array, that come out with specific total. For
> example, given the summation of array is 20. Then, I want to create an
> array with 1x5 matrix. The answer might be as follow:
> [5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.
>
> Thank you.

do you expect a special kind of distribution?
are negativ values allowed?

Subject: Creating array with specific sum

From: Mohd

Date: 22 Jul, 2011 11:14:11

Message: 4 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0blbi$s26$1@newscl01ah.mathworks.com>...
> "Mohd" wrote in message <j0bgc5$g05$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I want to create a 1xn array, that come out with specific total. For example, given the summation of array is 20. Then, I want to create an array with 1x5 matrix. The answer might be as follow:
> > [5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.
> >
>
> The bellow will create exactly the array as your requirement, however I'm almost sure it's not what you really want.
>
> >> a=rand(1,4)
>
> a =
>
> 0.8147 0.9058 0.1270 0.9134
>
> >> a(end+1)=20-sum(a)
>
> a =
>
> 0.8147 0.9058 0.1270 0.9134 17.2391
>
> Up to you to describe correctly the constraints, requirement that are still missing.
>
> Bruno

Thank you Bruno,

I want to create random +ve integer 1xn array with predefined summation.

Thanks again

Subject: Creating array with specific sum

From: Mohd

Date: 22 Jul, 2011 11:16:11

Message: 5 of 10

Lothar Schmidt <vapooroop@gmx.net> wrote in message <j0blml$d45$1@news.albasani.net>...
> Am 22.07.2011 11:37, schrieb Mohd:
> > Hi,
> >
> > I want to create a 1xn array, that come out with specific total. For
> > example, given the summation of array is 20. Then, I want to create an
> > array with 1x5 matrix. The answer might be as follow:
> > [5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.
> >
> > Thank you.
>
> do you expect a special kind of distribution?
> are negativ values allowed

No, just to create +ve random integer

Thanks

Subject: Creating array with specific sum

From: Mohd

Date: 22 Jul, 2011 11:17:09

Message: 6 of 10

Lothar Schmidt <vapooroop@gmx.net> wrote in message <j0blml$d45$1@news.albasani.net>...
> Am 22.07.2011 11:37, schrieb Mohd:
> > Hi,
> >
> > I want to create a 1xn array, that come out with specific total. For
> > example, given the summation of array is 20. Then, I want to create an
> > array with 1x5 matrix. The answer might be as follow:
> > [5 7 3 2 3], or [2 4 1 7 6] or any other 1x5 matrix with total 20.
> >
> > Thank you.
>
> do you expect a special kind of distribution?
> are negativ values allowed

No, just to create +ve random integer

Thanks

Subject: Creating array with specific sum

From: Bruno Luong

Date: 22 Jul, 2011 11:47:08

Message: 7 of 10

"Mohd" wrote in message <j0bm23$ci$1@newscl01ah.mathworks.com>...

If by integer you mean {1,2,3...}

s = 20; % target sum
m = 5; % size

a = randperm(s-1);
a = diff([0 sort(a(1:m-1)) s])

Bruno

Subject: Creating array with specific sum

From: Mohd

Date: 22 Jul, 2011 11:59:09

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0bnvr$5g8$1@newscl01ah.mathworks.com>...
> "Mohd" wrote in message <j0bm23$ci$1@newscl01ah.mathworks.com>...
>
> If by integer you mean {1,2,3...}
>
> s = 20; % target sum
> m = 5; % size
>
> a = randperm(s-1);
> a = diff([0 sort(a(1:m-1)) s])
>
> Bruno

Got it..thank you

Subject: Creating array with specific sum

From: Roger Stafford

Date: 23 Jul, 2011 07:29:09

Message: 9 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0bnvr$5g8$1@newscl01ah.mathworks.com>...
> "Mohd" wrote in message <j0bm23$ci$1@newscl01ah.mathworks.com>...
>
> If by integer you mean {1,2,3...}
>
> s = 20; % target sum
> m = 5; % size
>
> a = randperm(s-1);
> a = diff([0 sort(a(1:m-1)) s])
>
> Bruno
- - - - - - - - - - -
  The following is an alternative to Bruno's method. Its statistics are the same as his, namely that each possible solution has the same probability. Its only possible advantage would be for large values of s or m in that it doesn't have to do either of the two sort operations.

 a = ones(1,m);
 p = m;
 for k = s-1:-1:1
  if rand < (p-1)/k
   p = p - 1;
  else
   a(p) = a(p) + 1;
  end
 end

Roger Stafford

Subject: Creating array with specific sum

From: Bruno Luong

Date: 24 Jul, 2011 09:22:08

Message: 10 of 10

"Roger Stafford" wrote in message <j0dt85$3o$1@newscl02ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message

> The following is an alternative to Bruno's method. Its statistics are the same as his, namely that each possible solution has the same probability. Its only possible advantage would be for large values of s or m in that it doesn't have to do either of the two sort operations.
>
> a = ones(1,m);
> p = m;
> for k = s-1:-1:1
> if rand < (p-1)/k
> p = p - 1;
> else
> a(p) = a(p) + 1;
> end
> end
>

Roger's algorithm is a little gem.

Note that my algo can combine with Jan's shuffle based on Fisher-Yates algorithm

http://www.mathworks.com/matlabcentral/fileexchange/27076-shuffle

so as only a single sorting on an array of size m is enough.

Bruno

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