Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.fr>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Combinatorics question
Date: Thu, 29 Nov 2007 18:42:15 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 31
Message-ID: <fin167$k1b$1@fred.mathworks.com>
References: <fimps8$7in$1@fred.mathworks.com> <fimubj$76o$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.fr>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1196361735 20523 172.30.248.35 (29 Nov 2007 18:42:15 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 29 Nov 2007 18:42:15 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:440060


If you consider "natural number" starts from 0, call:
v=allVL1(n, L1);

If you consider "natural number" starts from 1, then call:
v=allVL1(n, L1-n)+1;

Bruno


function v=allVL1(n, L1)
% function v=allVL1(n, L1)
% INPUT
%   n: length of the vector
%   L1: desired L1 norm
% OUTPUT:
%   v: m x n array such as sum(v,2)=L1
%      all elements of v is naturel numbers {0,1,...}
%      v contains all possible combination

if n==1
    v = L1;
else
    v1 = (0:L1)';
    vrest = arrayfun(@(j) allVL1(n-1, L1-j),  v1, ...
        'UniformOutput', false);
    v = arrayfun(@(i) ...
        [repmat(v1(i),size(vrest{i},1),1) vrest{i}], ...
        (1:length(v1))',...
        'UniformOutput', false);
    v = cell2mat(v);
end