<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Good idea. Thank You!!</div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>--</div><div>Noah Silverman</div><div>UCLA Department of Statistics</div><div>8117 Math Sciences Building #8208</div><div>Los Angeles, CA 90095</div></div></div></span></div></span></div></span></div></span></span>
</div>
<br><div><div>On Sep 5, 2011, at 9:46 AM, Douglas Bates wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>On Mon, Sep 5, 2011 at 11:31 AM, Noah Silverman <<a href="mailto:noahsilverman@ucla.edu">noahsilverman@ucla.edu</a>> wrote:<br><blockquote type="cite">Hi,<br></blockquote><blockquote type="cite">Using Rcpp through inline.<br></blockquote><blockquote type="cite">I want to return a matrix, but I don't know the size beforehand. (My code<br></blockquote><blockquote type="cite">loops through a large data set accumulating certain statistics.)<br></blockquote><blockquote type="cite">With a NumericVector, I can use push_back() to just add values to the end of<br></blockquote><blockquote type="cite">the vector as they occur. Is there similar functionality for a<br></blockquote><blockquote type="cite">NumericMatrix?<br></blockquote><blockquote type="cite">Alternately, I could store all my generated statistics in several vectors<br></blockquote><blockquote type="cite">(6-7) and then, when complete with my loop, glue them together into a<br></blockquote><blockquote type="cite">matrix. But, this seems inefficient.<br></blockquote><br>Not really. The inefficient method is using push_back() on an Rcpp<br>object because it requires copying the existing vector to new storage<br>every time you add an element.<br><br>My suggestion is to use std::vector<double> objects to accumulate the<br>results, then allocate the Rcpp::NumericMatrix and copy the results<br>into columns of that matrix. Because the matrix will be in<br>column-major ordering you can do the copying using std::copy which is<br>reasonably efficient and less error prone than other methods.<br>However, this suggestion, like all other cases of determining<br>efficient ways to perform a calculation, should be benchmarked against<br>other methods.<br><br><blockquote type="cite">Here is a rough example of what I'm trying to do.<br></blockquote><blockquote type="cite">------------------------------<br></blockquote><blockquote type="cite">int n = inputData.size();<br></blockquote><blockquote type="cite">Rcpp::NumericMatrix output;<br></blockquote><blockquote type="cite">for(int i=0; i != n; i++){<br></blockquote><blockquote type="cite">// generate some stats with a lot of code not shown<br></blockquote><blockquote type="cite">foo = mean(stuff);<br></blockquote><blockquote type="cite">bar = min(stuff);<br></blockquote><blockquote type="cite">baz = max(stuff);<br></blockquote><blockquote type="cite">// etc...<br></blockquote><blockquote type="cite">output.push_back(foo, bar, baz);<br></blockquote><blockquote type="cite">)<br></blockquote><blockquote type="cite">return output;<br></blockquote><blockquote type="cite">-------------------------------<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">--<br></blockquote><blockquote type="cite">Noah Silverman<br></blockquote><blockquote type="cite">UCLA Department of Statistics<br></blockquote><blockquote type="cite">8117 Math Sciences Building #8208<br></blockquote><blockquote type="cite">Los Angeles, CA 90095<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">_______________________________________________<br></blockquote><blockquote type="cite">Rcpp-devel mailing list<br></blockquote><blockquote type="cite"><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br></blockquote><blockquote type="cite"><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote><blockquote type="cite"><br></blockquote></div></blockquote></div><br></body></html>