Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: find vector r<=p (p: a given vector) Date: Sat, 8 Nov 2008 01:25:03 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 35 Message-ID: <gf2ppf$q6l$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 1226107503 26837 172.30.248.38 (8 Nov 2008 01:25:03 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 8 Nov 2008 01:25:03 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1582760 Xref: news.mathworks.com comp.soft-sys.matlab:499623 Hello, I am asking questions again... I want to find all (vector) solutions for r<=p (component by component), where r, p are n-dimensional vectors, p is given, r is the unknown vector. In other words, find all vector r=(r_1, r_2, ...r_n) satisfying r_1 <=p_1, r_2 <=p_2, ... r_n <=p_n. Here is my code for this: n=length(p) R=[]; r= zeros(1,n); flag =1; while flag ==1 R = [R;r]; flag =0; for j= n:-1:1 if r(j)+1 <= p(j) r(j+1:n) =0; r(j) = r(j)+1; flag = 1; break end end end It does the job, but since I am not very experienced in Matlab, I wonder whether there're better ways than this double loop. Maybe there are some built-in functions that I should take adavantage of ? Thank you for your advice! Oriole