Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Creating array with specific sum
Date: Fri, 22 Jul 2011 11:02:10 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 24
Message-ID: <j0blbi$s26$1@newscl01ah.mathworks.com>
References: <j0bgc5$g05$1@newscl01ah.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1311332530 28742 172.30.248.35 (22 Jul 2011 11:02:10 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 22 Jul 2011 11:02:10 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:737151

"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