<div dir="ltr"><div><div><div>Hello, Amina.<br><br></div>Firstly, why are you even passing in a if b contains the proper dimensions. It's not like you are using a.<br><br></div>Secondly, and more importantly, once you provided the r code, you can see that this is **EXACTLY** the problem you asked on September 26 of this year, see <a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-September/009369.html">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-September/009369.html</a>, and the answer I gave on September 27 works here too, see <a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-September/009374.html">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-September/009374.html</a>.<br><br></div>Avi<br><br><div><div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 14, 2016 at 1:46 AM, Amina Shahzadi <span dir="ltr"><<a href="mailto:aminashahzadi@gmail.com" target="_blank">aminashahzadi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Oh sorry. No. of columns in b and size of a must always be same. </div><div>I have made an r code to show the output.</div><div><br></div><div><br></div><div>a = c(1, 2, 3)<br>b = matrix(1:30, nrow=10, ncol=3)<br>c = array(as.double(0), dim=c(10, 3, 10))<br>for(i in 1:10){<br> for(j in 1:3){<br> for(k in 1:i){<br> if(k==1) c[i, j, k] = b[i, j]<br> else c[i, j, k] = c[i-1, j, k-1] * b[i, j]<br> }<br> }<br>}<br></div><div><br></div><div>Output:</div><div><pre class="gmail-m_-3366529701800487838gmail-GCWXI2KCJKB" id="gmail-m_-3366529701800487838gmail-rstudio_console_output" style="font:10pt/13.63px "lucida console";margin:0px;border-width:medium;border-style:none;border-color:-moz-use-text-color;color:rgb(0,0,0);text-transform:none;text-indent:0px;letter-spacing:normal;word-spacing:0px;white-space:pre-wrap;word-break:break-all;background-color:rgb(255,255,255)"><span class="gmail-m_-3366529701800487838gmail-GCWXI2KCPKB gmail-m_-3366529701800487838gmail-ace_keyword" style="color:blue;white-space:pre-wrap">> </span><span class="gmail-m_-3366529701800487838gmail-GCWXI2KCPJB gmail-m_-3366529701800487838gmail-ace_keyword" style="color:blue">c
</span>, , 1
[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25
[6,] 6 16 26
[7,] 7 17 27
[8,] 8 18 28
[9,] 9 19 29
[10,] 10 20 30
, , 2
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 2 132 462
[3,] 6 156 506
[4,] 12 182 552
[5,] 20 210 600
[6,] 30 240 650
[7,] 42 272 702
[8,] 56 306 756
[9,] 72 342 812
[10,] 90 380 870
, , 3
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 6 1716 10626
[4,] 24 2184 12144
[5,] 60 2730 13800
[6,] 120 3360 15600
[7,] 210 4080 17550
[8,] 336 4896 19656
[9,] 504 5814 21924
[10,] 720 6840 24360
, , 4
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
[4,] 24 24024 255024
[5,] 120 32760 303600
[6,] 360 43680 358800
[7,] 840 57120 421200
[8,] 1680 73440 491400
[9,] 3024 93024 570024
[10,] 5040 116280 657720
, , 5
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
[4,] 0 0 0
[5,] 120 360360 6375600
[6,] 720 524160 7893600
[7,] 2520 742560 9687600
[8,] 6720 1028160 11793600
[9,] 15120 1395360 14250600
[10,] 30240 1860480 17100720
, , 6
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
[4,] 0 0 0
[5,] 0 0 0
[6,] 720 5765760 165765600
[7,] 5040 8910720 213127200
[8,] 20160 13366080 271252800
[9,] 60480 19535040 342014400
[10,] 151200 27907200 427518000
, , 7
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
[4,] 0 0 0
[5,] 0 0 0
[6,] 0 0 0
[7,] 5040 98017920 4475671200
[8,] 40320 160392960 5967561600
[9,] 181440 253955520 7866331200
[10,] 604800 390700800 10260432000
, , 8
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
[4,] 0 0 0
[5,] 0 0 0
[6,] 0 0 0
[7,] 0 0 0
[8,] 40320 1764322560 125318793600
[9,] 362880 3047466240 173059286400
[10,] 1814400 5079110400 235989936000
, , 9
[,1] [,2] [,3]
[1,] 0 0 0.000000e+00
[2,] 0 0 0.000000e+00
[3,] 0 0 0.000000e+00
[4,] 0 0 0.000000e+00
[5,] 0 0 0.000000e+00
[6,] 0 0 0.000000e+00
[7,] 0 0 0.000000e+00
[8,] 0 0 0.000000e+00
[9,] 362880 33522128640 3.634245e+12
[10,] 3628800 60949324800 5.191779e+12
, , 10
[,1] [,2] [,3]
[1,] 0 0 0.000000e+00
[2,] 0 0 0.000000e+00
[3,] 0 0 0.000000e+00
[4,] 0 0 0.000000e+00
[5,] 0 0 0.000000e+00
[6,] 0 0 0.000000e+00
[7,] 0 0 0.000000e+00
[8,] 0 0 0.000000e+00
[9,] 0 0 0.000000e+00
[10,] 3628800 670442572800 1.090274e+14
</pre><br class="gmail-m_-3366529701800487838gmail-Apple-interchange-newline"></div><div><br></div></div><div class="gmail_extra"><div><div class="gmail-h5"><br><div class="gmail_quote">On Wed, Dec 14, 2016 at 7:27 PM, Avraham Adler <span dir="ltr"><<a href="mailto:avraham.adler@gmail.com" target="_blank">avraham.adler@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-m_-3366529701800487838HOEnZb"><div class="gmail-m_-3366529701800487838h5"><div><div><br></div></div><div><br><div class="gmail_quote"><div>On Wed, Dec 14, 2016 at 1:24 AM Amina Shahzadi <<a href="mailto:aminashahzadi@gmail.com" target="_blank">aminashahzadi@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Hello Avraham --Happy to see you</div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">My code is trying to produce a cube c which is going to be constructed by a vector a and matrix b.</div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">And the number of rows in b and size of a must be same.</div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">So we can assume that if a is a vector of size 3, Then b must be 2 x 3 or 3 X 3 etc. </div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Thank you Avraham for quick response. I hope this will make my question more clear.</div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Best regards</div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div></div><div class="gmail_extra gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail_extra gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail_quote gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">On Wed, Dec 14, 2016 at 4:46 PM, Avraham Adler <span class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><<a class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" href="mailto:avraham.adler@gmail.com" target="_blank">avraham.adler@gmail.com</a>></span> wrote:<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><blockquote class="gmail_quote gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail_extra gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail_quote gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726m_1426088852283644643h5 gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi <span class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><<a class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" href="mailto:aminashahzadi@gmail.com" target="_blank">aminashahzadi@gmail.com</a>></span> wrote:<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><blockquote class="gmail_quote gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Hello Friends and Prof. Dirk<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div>I am pasting here a code which has a for loop depending on another for loop. <br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div>I am getting zeros for cube c. I tried and searched a lot but did not get an example of this type. Would you please help in this regard?<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">#include <RcppArmadillo.h><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">using namespace Rcpp;<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">using namespace RcppArmadillo;<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">using namespace arma;<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">//[[Rcpp::depends(RcppArmadill<wbr>o)]]<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">//[[Rcpp::export]]<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">arma::cube exam(arma::vec a, arma::mat b)<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">{<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> int m = a.size();<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> int n = b.n_rows;<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> arma::cube c = zeros<cube>(n, m, n);<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> for(int i=0; i<n; i++) {<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> for(int j=0; j<m; j++) {<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> for(int k=0; k<i; k++) {<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> if(k==0) {<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> c(i, j ,k) = c(i, j, k) + b(i, j);<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> }<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> else{<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> c(i, j, k) = c(i, j, k) + c(i-1, j, k) *b(i, j);<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> }<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> }<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> }<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> }<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"> return c;<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">}<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Thank You<span class="gmail-m_-3366529701800487838m_5616406869366001726m_1426088852283644643m_-460033997963071725gmail-HOEnZb gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><font class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" color="#888888"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></font></span></div><span class="gmail-m_-3366529701800487838m_5616406869366001726m_1426088852283644643m_-460033997963071725gmail-HOEnZb gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><font class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" color="#888888"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">-- <br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726m_1426088852283644643m_-460033997963071725gmail-m_-5403746280849366164gmail_signature gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><i class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><font class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" face="comic sans ms,sans-serif">Amina Shahzadi</font></i></div><br><br></div></font></span></div></div></div></div></blockquote></div></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Hello.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">I haven't run your code, but it strikes me<br><br> that I cannot see where are you capturing the number of columns of b. <br><br>It's a bit confusing as I was always taught a matrix has m rows and n <br><br>columns. Be that as it may, your k==0 loop looks like it's trying to <br><br>copy over the original matrix to the first slice, but how do you know <br><br>that b has m columns, which is what you're assuming by letting j loop to<br><br> m. Unless you are assuming a square matrix?<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Even if you are, if your matrix is not the same length as your vector, I think there is an issue with your loop boundaries, unless I've misunderstood something, which is certainly possible.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">For example, assume a is {1, 2, 3} and b is the 2 x 2 of row 1: [1 2] and row 2: [3 4]. Thus m = 3 and n = 2.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Step 1: i = j = k = 0: c(0, 0, 0) becomes b(0, 0) or 1.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Step 2: i = 0, j = 1, k = 0: c(0, 1, 0) becomes b(0, 1) or 2.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Step 3: i = 0, j = 2, k = 0: c(0, 2, 0) becomes b(0, 2) ?!?! There is no b(0, 2), it's only a 2x2 matrix?<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">Similar to your previous questions, instead of posting code, can you please describe in words what it is you are trying to do? That may help.<br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div>Avi <br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div></div><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div></div><br><br></blockquote></div><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" clear="all"><br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"></div><div class="gmail_extra gmail-m_-3366529701800487838m_5616406869366001726gmail_msg">-- <br class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><div class="gmail-m_-3366529701800487838m_5616406869366001726m_1426088852283644643gmail_signature gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><i class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg"><font class="gmail-m_-3366529701800487838m_5616406869366001726gmail_msg" face="comic sans ms,sans-serif">Amina Shahzadi</font></i></div><br><br></div></blockquote></div></div></div></div>Constructed how? Can you provide a simple set of inputs and the expected output? <div><br></div><div>Avi</div><span class="gmail-m_-3366529701800487838HOEnZb"><font color="#888888"><div dir="ltr">-- <br></div><div>Sent from Gmail Mobile</div>
</font></span></blockquote></div><br><br clear="all"><br></div></div><span class="gmail-HOEnZb"><font color="#888888">-- <br><div class="gmail-m_-3366529701800487838gmail_signature"><i><font face="comic sans ms,sans-serif">Amina Shahzadi</font></i></div>
</font></span></div>
</blockquote></div><br></div></div></div></div></div></div></div>