<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871</link>
    <title>MATLAB Central Newsreader - find vector r&lt;=p (p: a given vector)</title>
    <description>Feed for thread: find vector r&lt;=p (p: a given vector)</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 08 Nov 2008 01:25:03 -0500</pubDate>
      <title>find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#609699</link>
      <author>Oriole </author>
      <description>Hello, &lt;br&gt;
&lt;br&gt;
I am asking questions again...&lt;br&gt;
&lt;br&gt;
I want to find all (vector) solutions for r&amp;lt;=p (component by component), where r, p are n-dimensional vectors, p is given, r is the unknown vector. &lt;br&gt;
&lt;br&gt;
In other words, find all vector r=(r_1, r_2, ...r_n) satisfying r_1 &amp;lt;=p_1, r_2 &amp;lt;=p_2, ... r_n &amp;lt;=p_n. &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Here is my code for this:&lt;br&gt;
&lt;br&gt;
n=length(p)&lt;br&gt;
R=[];&lt;br&gt;
r= zeros(1,n);&lt;br&gt;
flag =1;&lt;br&gt;
while  flag ==1   &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;R = [R;r];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;flag =0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j= n:-1:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if r(j)+1 &amp;lt;= p(j)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r(j+1:n) =0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r(j) = r(j)+1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;flag = 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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 ?&lt;br&gt;
&lt;br&gt;
Thank you for your advice!&lt;br&gt;
Oriole</description>
    </item>
    <item>
      <pubDate>Thu, 20 Nov 2008 21:48:01 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#612233</link>
      <author>Bruno Luong</author>
      <description>% Data&lt;br&gt;
p=round(9*rand(1,5))+1;&lt;br&gt;
&lt;br&gt;
% Engine&lt;br&gt;
c=arrayfun(@(k) 0:p(k), 1:length(p), 'UniformOutput', false);&lt;br&gt;
[c{:}]=ndgrid(c{:});&lt;br&gt;
R=reshape(cat(length(p)+1,c{:}),[],length(p));&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 00:23:01 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#612771</link>
      <author>Roger Stafford</author>
      <description>&quot;Oriole &quot; &amp;lt;oriole_ni@hotmail.com&amp;gt; wrote in message &amp;lt;gf2ppf$q6l$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am asking questions again...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to find all (vector) solutions for r&amp;lt;=p (component by component), where r, p are n-dimensional vectors, p is given, r is the unknown vector. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In other words, find all vector r=(r_1, r_2, ...r_n) satisfying r_1 &amp;lt;=p_1, r_2 &amp;lt;=p_2, ... r_n &amp;lt;=p_n. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here is my code for this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; n=length(p)&lt;br&gt;
&amp;gt; R=[];&lt;br&gt;
&amp;gt; r= zeros(1,n);&lt;br&gt;
&amp;gt; flag =1;&lt;br&gt;
&amp;gt; while  flag ==1   &lt;br&gt;
&amp;gt;     R = [R;r];&lt;br&gt;
&amp;gt;     flag =0;&lt;br&gt;
&amp;gt;     for j= n:-1:1&lt;br&gt;
&amp;gt;         if r(j)+1 &amp;lt;= p(j)&lt;br&gt;
&amp;gt;             r(j+1:n) =0;&lt;br&gt;
&amp;gt;             r(j) = r(j)+1;&lt;br&gt;
&amp;gt;             flag = 1;&lt;br&gt;
&amp;gt;             break&lt;br&gt;
&amp;gt;         end&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 ?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your advice!&lt;br&gt;
&amp;gt; Oriole&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Just for variety's sake here is a for-loop method, Oriole.  It should be much faster than your double loop.  Bruno's vectorized solution is certainly more elegant-looking and could well be even faster.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;n = length(p);&lt;br&gt;
&amp;nbsp;d = max(floor(p),0);&lt;br&gt;
&amp;nbsp;R = (0:d(n))';&lt;br&gt;
&amp;nbsp;s = d(n)+1;&lt;br&gt;
&amp;nbsp;for k = n-1:-1:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;R = repmat(R,d(k)+1,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;t = repmat(0:d(k),s,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;s = s*(d(k)+1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;R = [t(:),R];&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 21:55:04 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#613025</link>
      <author>Matt Fig</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &lt;br&gt;
&amp;gt;   Just for variety's sake here is a for-loop method, Oriole.  It should be much faster &lt;br&gt;
&amp;gt; than your double loop.  Bruno's vectorized solution is certainly more elegant-looking &amp;gt; and could well be even faster.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  n = length(p);&lt;br&gt;
&amp;gt;  d = max(floor(p),0);&lt;br&gt;
&amp;gt;  R = (0:d(n))';&lt;br&gt;
&amp;gt;  s = d(n)+1;&lt;br&gt;
&amp;gt;  for k = n-1:-1:1&lt;br&gt;
&amp;gt;   R = repmat(R,d(k)+1,1);&lt;br&gt;
&amp;gt;   t = repmat(0:d(k),s,1);&lt;br&gt;
&amp;gt;   s = s*(d(k)+1);&lt;br&gt;
&amp;gt;   R = [t(:),R];&lt;br&gt;
&amp;gt;  end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I liked Roger's approach, except that the R is not pre-allocated and is built-up through repmatting.  I fixed this.   Using timeit this version shows a 2.5 to 3.2 times gain in speed depending on p.  Here is my for-loop approach:&lt;br&gt;
&lt;br&gt;
sz = prod(p+1); % The final array size.&lt;br&gt;
[idx,idx] = sort(p,'descend'); % Largest to smallest.&lt;br&gt;
R = zeros(sz,length(p)); % Preallocation.&lt;br&gt;
y = zeros(sz,1); % Preallocation.&lt;br&gt;
&lt;br&gt;
for kk = idx % Fill R in this order.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;div = sz;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sz = sz/(p(kk)+1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(1:sz:end) = 1; % Poke in ones.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(div+1:div:end) = -p(kk); %  Poke in drops.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;R(:,kk) = cumsum(y) - 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(:) = 0;  % Reset for the next pass.&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 15:35:02 -0500</pubDate>
      <title>Bruno, Roger, Matt</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622525</link>
      <author>Oriole </author>
      <description>Thank you so much!.&lt;br&gt;
&lt;br&gt;
I got tired of my matlab problems so I took a long break.. but now I start to look at them again..&lt;br&gt;
&lt;br&gt;
I tested and studied all of your code, and they are really so much more efficient (and professional) than mine, really like them!&lt;br&gt;
&lt;br&gt;
I have also noticed that, when the vector p is large in terms of dimension and the components, my code almost crush (too slow).  &lt;br&gt;
&lt;br&gt;
/Oriole</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 15:39:02 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622526</link>
      <author>Oriole </author>
      <description>&quot;Matt Fig&quot; &amp;lt;spamanon@yahoo.com&amp;gt; wrote in message &amp;lt;ggf7ro$fal$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt;   Just for variety's sake here is a for-loop method, Oriole.  It should be much faster &lt;br&gt;
&amp;gt; &amp;gt; than your double loop.  Bruno's vectorized solution is certainly more elegant-looking &amp;gt; and could well be even faster.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;  n = length(p);&lt;br&gt;
&amp;gt; &amp;gt;  d = max(floor(p),0);&lt;br&gt;
&amp;gt; &amp;gt;  R = (0:d(n))';&lt;br&gt;
&amp;gt; &amp;gt;  s = d(n)+1;&lt;br&gt;
&amp;gt; &amp;gt;  for k = n-1:-1:1&lt;br&gt;
&amp;gt; &amp;gt;   R = repmat(R,d(k)+1,1);&lt;br&gt;
&amp;gt; &amp;gt;   t = repmat(0:d(k),s,1);&lt;br&gt;
&amp;gt; &amp;gt;   s = s*(d(k)+1);&lt;br&gt;
&amp;gt; &amp;gt;   R = [t(:),R];&lt;br&gt;
&amp;gt; &amp;gt;  end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I liked Roger's approach, except that the R is not pre-allocated and is built-up through repmatting.  I fixed this.   Using timeit this version shows a 2.5 to 3.2 times gain in speed depending on p.  Here is my for-loop approach:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; sz = prod(p+1); % The final array size.&lt;br&gt;
&amp;gt; [idx,idx] = sort(p,'descend'); % Largest to smallest.&lt;br&gt;
&amp;gt; R = zeros(sz,length(p)); % Preallocation.&lt;br&gt;
&amp;gt; y = zeros(sz,1); % Preallocation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for kk = idx % Fill R in this order.&lt;br&gt;
&amp;gt;     div = sz;&lt;br&gt;
&amp;gt;     sz = sz/(p(kk)+1);&lt;br&gt;
&amp;gt;     y(1:sz:end) = 1; % Poke in ones.&lt;br&gt;
&amp;gt;     y(div+1:div:end) = -p(kk); %  Poke in drops.&lt;br&gt;
&amp;gt;     R(:,kk) = cumsum(y) - 1;&lt;br&gt;
&amp;gt;     y(:) = 0;  % Reset for the next pass.&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Hi, Matt, &lt;br&gt;
I like this solution and I have implemented line by line in the matlab. It works so nicely but still I can't figure out the basic idea behind the code. Can you explain a little bit the algorithm for me?? I would really appreciate that help!&lt;br&gt;
Oriole</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 22:14:02 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622613</link>
      <author>Oriole </author>
      <description>&quot;Oriole &quot; &amp;lt;oriole_ni@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; Hi, Matt, &lt;br&gt;
&amp;gt; I like this solution and I have implemented line by line in the matlab. It works so nicely but still I can't figure out the basic idea behind the code. Can you explain a little bit the algorithm for me?? I would really appreciate that help!&lt;br&gt;
&amp;gt; Oriole&lt;br&gt;
&lt;br&gt;
Matt, helloooo.....</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 22:32:15 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622616</link>
      <author>Matt Fig</author>
      <description>Oh, hello.  I didn't see this old one had been resurrected!&lt;br&gt;
&lt;br&gt;
Basically my approach was to build the index for cumsum (a technique I have seen many others use).  If you run the code without the semicolon on the line that creates the drops for y you can see the pattern.  To use this method you need to understand how cumsum works, and look for this type of pattern.  Since the result from cumsum is off by one, I just subtracted one from the vector before storing it in R.  &lt;br&gt;
&lt;br&gt;
The main speed advantage, as far as loop is concerned, is pre-allocating the matrix R and y.  Also, Matlab's JIT likes all variables to stay the same size and type during a loop, so that is why I set y to zero at the end of each iteration.  &lt;br&gt;
&lt;br&gt;
Hope that clears things up some :)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
e!R](z882.'y%$8.{&quot;X/F%}})&amp;8&amp;(&quot;?(} y-8yy(-y}(z{q'8(y8?-)y!8!</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 22:45:04 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622618</link>
      <author>Oriole </author>
      <description>&quot;Matt Fig&quot; &amp;lt;spamanon@yahoo.com&amp;gt; wrote in message &amp;lt;gl2v1f$cmg$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Oh, hello.  I didn't see this old one had been resurrected!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Basically my approach was to build the index for cumsum (a technique I have seen many others use).  If you run the code without the semicolon on the line that creates the drops for y you can see the pattern.  To use this method you need to understand how cumsum works, and look for this type of pattern.  Since the result from cumsum is off by one, I just subtracted one from the vector before storing it in R.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The main speed advantage, as far as loop is concerned, is pre-allocating the matrix R and y.  Also, Matlab's JIT likes all variables to stay the same size and type during a loop, so that is why I set y to zero at the end of each iteration.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope that clears things up some :)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; e!R](z882.'y%$8.{&quot;X/F%}})&amp;8&amp;(&quot;?(} y-8yy(-y}(z{q'8(y8?-)y!8!&lt;br&gt;
&lt;br&gt;
Thank you, Matt. hmm.. this cumsum function really confuses me, I know technically what it does, it culmulatively sums the values up column-wise, but why do this? I still don't see the big picture.. but I think more about it tomorrow.  In case you still have patience, what's the purpose of these &quot;drops&quot; ?  &lt;br&gt;
&lt;br&gt;
Oriole</description>
    </item>
    <item>
      <pubDate>Mon, 19 Jan 2009 23:04:01 -0500</pubDate>
      <title>Re: find vector r&lt;=p (p: a given vector)</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238871#622621</link>
      <author>Matt Fig</author>
      <description>&lt;br&gt;
&amp;gt; Thank you, Matt. hmm.. this cumsum function really confuses me, I know technically what it does, it culmulatively sums the values up column-wise, but why do this? I still don't see the big picture.. but I think more about it tomorrow.  In case you still have patience, what's the purpose of these &quot;drops&quot; ?  &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The drops are to reset the cumulative sum.  If we didn't put in drops, we couldn't get the repeating pattern out of cumsum.  For example, cumsum this guy:&lt;br&gt;
&lt;br&gt;
x = [1 0 0 1 0 0 -1 0 0 1 0 0 ]&lt;br&gt;
&lt;br&gt;
See the pattern?  We couldn't get the pattern to go 111222 then back to 111222 without adding a negative number (or drop) for the cumsum. &lt;br&gt;
&lt;br&gt;
As far as the big picture, it is simply seeing the pattern of the final return from Bruno's code (I think it was) then trying to get that with cumsum.  Note that we could have put the cumsum outside the loop, just store the y's in their correct columns in R, then cumsum R after the loop.  I don't know if this would really make that much difference time wise. &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
As with any piece of confusing or murky code, just spend some time looking at the output of various stages, you'll get it... &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
0$!$+t+|;Bh1|!0}`;|1(|;|;+B,+,;~%!$0}[2|);(;*';5~+#I)U+|*%!</description>
    </item>
  </channel>
</rss>

