Newest questions tagged list-manipulation - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-20T10:41:48Z https://mathematica.stackexchange.com/feeds/tag?tagnames=list-manipulation&sort=newest http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/203977 4 Collect data points in a list into weekly groups? Kagaratsch https://mathematica.stackexchange.com/users/5517 2019-08-20T03:50:06Z 2019-08-20T06:52:03Z <p>Consider data of a format such as</p> <pre><code>data = {{{2002, 7, 30}, 1.20431}, {{2002, 7, 31}, 0.52822}, {{2002, 8, 1}, -1.51937}, {{2002, 8, 2}, -1.2966}, {{2002, 8, 5}, -1.97334}, {{2002, 8, 6}, 1.07311}, {{2002, 8, 7}, 0.224605}, {{2002, 8, 8}, 1.78266}, {{2002, 8, 9}, 0.90241}, {{2002, 8, 12}, 0.502187}}; </code></pre> <p>I'd like to have a function that adds a dimension to the list and automatically collects all data points belonging to a single week together, e.g.:</p> <pre><code>weekly[data] </code></pre> <p>with output</p> <pre><code> { { {{2002, 7, 30}, 1.20431}, {{2002, 7, 31}, 0.52822}, {{2002, 8, 1}, -1.51937}, {{2002, 8, 2}, -1.2966} } , { {{2002, 8, 5}, -1.97334}, {{2002, 8, 6}, 1.07311}, {{2002, 8, 7}, 0.224605}, {{2002, 8, 8}, 1.78266}, {{2002, 8, 9}, 0.90241} } , { {{2002, 8, 12}, 0.502187} } }; </code></pre> <blockquote> <p>Is there a function in Mathematica that can do this? Or maybe one can implement it?</p> </blockquote> https://mathematica.stackexchange.com/q/203913 7 How best to join tables, which have different lengths on the same column values which exist in both tables? QuantumPenguin https://mathematica.stackexchange.com/users/27119 2019-08-18T16:36:29Z 2019-08-19T18:20:09Z <p>I think my problem is pretty simple, and in SQL this would be trivial. I have two tables</p> <pre><code>TableOne = {{a, x1}, {b, x2}, {c, x3}}; TableTwo = {{a, y1}, {c, y2} , {a, y3}, {a, y4}, {b, y5}, {c,y6}, {c, y7}} </code></pre> <p>I want to abe able to join these two tables where the values of column 1 in both tables match such that:</p> <pre><code>DesiredResult = {{a, x1, a, y1}, {c, x3 , c, y2} , {a, x1, a, y3}, {a, x1, a, y4}, {b, x2, b, y5}, {c, x3, c, y6}, {c, x3, c, y7}} </code></pre> <p>I've tried with both <code>Select[]</code> statements inside a <code>Table[]</code> structure and also looked into <code>JoinAcross[]</code> but haven't been able to acheive the desire effect. In SQL it would be simple, something like:</p> <pre><code>SELECT Col1.Table1, Col2.Table1, Col1.Table2, Col2.Table2, FROM table2 INNER JOIN table1 ON Col1.Table1 = Col1.Table2 </code></pre> <p>Or something similar.</p> https://mathematica.stackexchange.com/q/203890 5 Show two plots together: a two dimensional curve tangent to the maxima of a three dimensional plot Unbelievable https://mathematica.stackexchange.com/users/14527 2019-08-18T07:16:27Z 2019-08-18T17:39:26Z <p>I have a list containing <code>3</code> columns and <code>6552</code> rows which can be found <a href="https://ufile.io/xsleyvmr" rel="nofollow noreferrer">here</a>.</p> <p>The plot of data is shown below:</p> <p><a href="https://i.stack.imgur.com/9Etfv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Etfv.png" alt="Enter image description here"></a></p> <p>We have a cross cut for a specific value of <code>y</code>, for example, <code>y=0.01 or y=1.26</code> over this plot by (<code>mathematica.dat</code> is the uploaded file):</p> <pre><code>data3D = Import["mathematica.dat", "Table"]; yequalto01 = Rest /@ (Select[data3D, #[] == 0.01 &amp;]); yequalto126 = Rest /@ (Select[data3D, #[] == 1.26 &amp;]); </code></pre> <p>From a two column data (<code>yequalto01</code>,<code>yequalto126</code>) we can extract the peaks by <code>peakValues = Pick[yequalto01, PeakDetect[yequalto01[[;; , 2]]], 1];</code></p> <p>This list except the last pair can be presented as</p> <pre><code>peakValues ={{0.0, 1.000}, {4.4, 0.982}, {8.9, 0.961}, {13.3, 0.942}, {17.8, 0.923}, {22.2, 0.906}}; </code></pre> <p>We plotted <code>peakValues</code> and drawn <code>data3D</code> separately. But, I wish to show two plots simultaneous in one figure as the below one which prepared by paint (blue and purple curves are tangent to the peaks of the 3D plot).</p> <p><a href="https://i.stack.imgur.com/ch6fF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ch6fF.png" alt="Enter image description here"></a></p> https://mathematica.stackexchange.com/q/203878 10 What is the best way to use errors in mathematica M12? QuantumPenguin https://mathematica.stackexchange.com/users/27119 2019-08-17T18:29:47Z 2019-08-17T19:41:57Z <p>Since <code>ErrorListPlot</code> has been superseded by new functionality in <code>ListPlot</code> for Mathematica 12.0, what is the best way to handle errors efficiently? Previously I would simply format my data in a table as</p> <pre><code> Data = Table[ Stuf to calculate values and errors {{xValue, yValue, yError}}, {i, 1, Stop} ] </code></pre> <p>Which made it easy to stick into <code>ErrorListPlot</code> or use in a fit as</p> <pre><code>NonlinearModel[Data[[1;;,{1, 2}]], Function, Weights-&gt;1/Data[[1;;,3]]^2 ] </code></pre> <p>Now it seems you have to use <code>Around[yValue, yError]</code></p> <pre><code> Data = Table[ Stuf to calculate values and errors {{xValue, Around[yValue, yError]}}, {i, 1, Stop} ] </code></pre> <p>Which works fine in <code>ListPlot</code> and I can fit data with it, as in it produces a fit result, but I can't figure out how to use <code>Weights</code> with <code>Around</code> and I can't plot the result of the fit in the usual way. Can anyone recommend an efficient way to format data in line with the new updates?</p> https://mathematica.stackexchange.com/q/203861 1 Problems with the parametric Recurrence Table Kass https://mathematica.stackexchange.com/users/7333 2019-08-17T12:05:38Z 2019-08-17T13:05:54Z <p>I am trying to construct a minimization program where I have to minimize a function with respect to a matrix. This matrix enters into the optimization program in a convoluted manner together with data. My problem is that I cannot construct this part of the function. </p> <p>What I have tried: </p> <p>First generate some fake data:</p> <pre><code> A = {{5, 4, 1}, {4, 7, 3}, {1, 3, 5}}; B = {{3, 2, -1}, {2, 5, 1}, {-1, 1, 3}}; Xsam = RandomVariate[MultinormalDistribution[{0, 0, 0}, B], 1000]; Ysam = RandomVariate[MultinormalDistribution[{0, 0, 0}, A], 1000]; </code></pre> <p>Generate the parametric matrix</p> <pre><code> M0 = Array[Subscript[a, ##] &amp;, {3, 3}]; </code></pre> <p>The first step in creating the final function of M, Xsam, Ysam is:</p> <pre><code> r[X_,Y_,M_]:=Exp[X.M.Transpose[Y]]; </code></pre> <p>Then, for </p> <pre><code>pX=1/1000; pY=1/1000; r1=r[Xsam,Ysam,M0] </code></pre> <p>I would like to take the 10th of iteration of a recurrence relationship defined in this table (I already use "Take") which should be a function of M0</p> <pre><code>Take[RecurrenceTable[{b[n + 1] == pY/(Transpose[r1].a[n]), a[n + 1] == pX/(r1.b[n]), a == Exp[-ConstantArray[1, Length [Xsam]]], b == Exp[-ConstantArray[1, Length [Xsam]]]}, {a, b}, {n, 1, 10}], {10}] </code></pre> <p>However it does not work (I get the message "RecurrenceTable: The function appears with no arguments").</p> <p>If I use instead of M0 some numerically valued matrix, it does work (even if surprisingly slow, even if on 32G RAM PC). </p> <p>Thank you for any hints as to how to get to the 10th iteration (and make it defined as a function of elements of M0) on which I will be later optimizing on. </p> https://mathematica.stackexchange.com/q/203827 7 Select elements of a list by comparing it to another list William https://mathematica.stackexchange.com/users/2871 2019-08-16T15:10:52Z 2019-08-18T03:32:58Z <p>Suppose I have the following lists: </p> <pre><code>prod = {{"x1", {"a", "b", "c", "d"}}, {"x2", {"e", "f", "g"}}, {"x3", {"h", "i", "j", "k", "l"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}} </code></pre> <p>and </p> <pre><code>sub = {{"m", "n"}, {"o", "p", "r", "q"}, {"g", "f", "e"}}; </code></pre> <p>for each element in <code>sub</code> I want to go through <code>prod</code> and select if the element exist such that I get the following output, </p> <pre><code> {{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q","r"}}} </code></pre> <p>I tried doing: </p> <pre><code>Table[Select[ prod[[All, 2]][[i]], # == ContainsAny[Map[Sort, sub]][[i]] &amp;], {i, Length[sub]}] </code></pre> <p>yet it doesn't work, am I missing something? </p> https://mathematica.stackexchange.com/q/203819 3 How to use indices during Map? Charlie https://mathematica.stackexchange.com/users/66993 2019-08-16T13:25:00Z 2019-08-18T03:31:16Z <p>I want to map a function on each element at the "deepest" level of a list.</p> <p>The list can have an arbitrary number of dimensions.</p> <p>The function needs the indices of the element to which it is being mapped.</p> <p>How can I determine the indices of an element during mapping ?</p> https://mathematica.stackexchange.com/q/203813 4 How do I pass a "list of lists" as the argument to a function of the form F[x,y]? [duplicate] Manoj Raghavendran https://mathematica.stackexchange.com/users/67009 2019-08-16T12:51:06Z 2019-08-16T14:30:03Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/1970/using-sets-of-arguments-from-a-list" dir="ltr">Using sets of arguments from a list</a> <span class="question-originals-answer-count"> 3 answers </span> </li> </ul> </div> <p>I have a function <code>F[x,y]</code> and a list of the form:</p> <pre><code>{{a1,b1},{a2,b2},{a3,b3},{an,bn}} </code></pre> <p>Basically, I want to pass the list mentioned above as the argument to the function <code>F</code> and get the output as a list of the form: </p> <pre><code>{F[a1,b1],F[a2,b2],F[a3,b3],F[an,bn]} </code></pre> <p>How do I do this?</p> https://mathematica.stackexchange.com/q/203760 5 List, map function based on a condition Michiel van Mens https://mathematica.stackexchange.com/users/9590 2019-08-15T13:00:41Z 2019-08-15T17:32:33Z <p>I have the following list:</p> <pre><code>input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1}} </code></pre> <p>there are two conditions:</p> <ul> <li>When a '0' is followed by '1', the value of '0' change to .5 </li> <li>When a '1' is followed by '0', the value of '0' change to .5</li> </ul> <p>the desired output is:</p> <pre><code>output = [{{0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5, 0, .5, 1, .5, 1}, {.5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1, 1, .5, 1, .5, 0, .5, 1, .5, 1}, {1, 1, .5, .5, 1, 1, 1, .5, 0, 0, 0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1}}] </code></pre> <p>Who has a suggestion how to get the desired output</p> https://mathematica.stackexchange.com/q/203728 1 Numerically convolve lists in mathematica QuantumPenguin https://mathematica.stackexchange.com/users/27119 2019-08-14T15:19:44Z 2019-08-15T14:12:37Z <p>I have a function <span class="math-container">$$\chi(x) = \frac{a^2}{a^2 + \left(\frac{d^{2}}{x} + x\left( \frac{ab}{x^{2} - c^2} - 1 \right) \right)^{2}}$$</span> which I would like to convolve with the Fourier transform of the Hanning window, <span class="math-container">$H(t)\rightarrow\tilde{H}(\omega)$</span>. I have attempted to do this 'analytically' in Mathematica with </p> <pre><code>Convolve[\chi[x], FourierTransform[HanningWindow[t], t, x], x, y] </code></pre> <p>However this never completes even with assumptions, such as <code>Assumptions-&gt; x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; a &gt; 0 &amp;&amp; b &gt; 0 &amp;&amp; c &gt; 0</code>. Maybe this expression is too difficult, there is maybe no closed form or maybe I don't know enough about how to use <code>Convolve[...]</code> effectively. </p> <p>If anyone has any thoughts about this problem that would be very helpful. Here is the function in, <span class="math-container">$\chi(x)$</span>, Mathematica code:</p> <pre><code>\[Chi][a_, b_, c_, d_, x_] := a^2/( a^2 + (d^2/x + x ((a b)/(x^2 - c^2) - 1))^2) </code></pre> <p>My next approach is to do this numerically. So I will generate two lists one for <span class="math-container">$\chi(x)$</span>, where I define values for <span class="math-container">$a$</span>, <span class="math-container">$b$</span>, and <span class="math-container">$c$</span>, and one list for <span class="math-container">$\tilde{H}(\omega)$</span>. I choose some arbitary resolution in <span class="math-container">$\omega$</span> as <span class="math-container">$\delta \omega$</span>.</p> <p>I then want to make the convolution of the two lists <span class="math-container">$$\{\chi(\omega_{i})\} * \{\tilde{H}(\omega_{i})\}$$</span>, and maybe this will give me the information I am interested in.</p> <p>I haven't been able to discover how to make such a convolution with lists in Mathematica -- is this possible? </p> https://mathematica.stackexchange.com/q/203694 2 Do not skip empty table entries while importing data from HTML string? Kagaratsch https://mathematica.stackexchange.com/users/5517 2019-08-13T20:55:34Z 2019-08-13T22:07:19Z <p>Consider importing the following table from an HTML string:</p> <pre><code>ImportString["&lt;table&gt; &lt;tr&gt; &lt;th&gt;Firstname&lt;/th&gt; &lt;th&gt;Lastname&lt;/th&gt; &lt;th&gt;Age&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Jill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;50&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;", {"HTML", "Data"}] </code></pre> <blockquote> <p>{{"Firstname", "Lastname", "Age"}, {"Jill", "Smith", 50}}</p> </blockquote> <p>above, all fields are imported and we end up with a proper 2x3 table.</p> <p>However, in a case where one of the entries is empty, we instead get:</p> <pre><code>ImportString["&lt;table&gt; &lt;tr&gt; &lt;th&gt;Firstname&lt;/th&gt; &lt;th&gt;Lastname&lt;/th&gt; &lt;th&gt;Age&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;50&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;", {"HTML", "Data"}] </code></pre> <blockquote> <p>{{"Firstname", "Lastname", "Age"}, {"Smith", 50}}</p> </blockquote> <p>Here the "Firstname" field was left empty in the table, and so mathematica did not register that field. The resulting list now has 3 entries in the first dimension and only 2 entries in the second, so that the whole table structure is broken. I would prefer the output to instead read e.g.</p> <pre><code>{{"Firstname", "Lastname", "Age"}, {"","Smith", 50}} </code></pre> <p>so that table dimensions are preserved.</p> <blockquote> <p>Is there a way to import table data from HTML string without skipping empty fields?</p> </blockquote> https://mathematica.stackexchange.com/q/203638 0 Nonlinear fitting does not perform appropriately Inzo Babaria https://mathematica.stackexchange.com/users/33967 2019-08-12T17:46:40Z 2019-08-12T21:41:35Z <p>I have a list as below one:</p> <pre><code> mustbefitted={{0., 1.}, {4.4, 0.982211}, {8.9, 0.961575}, {13.3, 0.942571}, {17.8, 0.923857}, {22.2, 0.906203}, {25.1, 0.046994}}; </code></pre> <p>I wish to fit the list with a function as <code>a - b t^c</code></p> <pre><code>NonlinearModelFit[mustbefitted, a - b t^c, {a, b, c}, t]; </code></pre> <p>But unfortunately the above fitting command does not perform correctly and I could not understand the mean of the massage after running the function i.e., <code>The Jacobian is not a matrix of real numbers at {a,b,c} = {1.,1.,1.}</code>!!!</p> https://mathematica.stackexchange.com/q/203635 1 Select lists that don't contain a given element (was: elements in a list (all elements should be satisfied)) [duplicate] zongxian https://mathematica.stackexchange.com/users/21553 2019-08-12T17:03:59Z 2019-08-12T17:17:44Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/97210/how-to-select-sub-lists-having-at-least-one-element-that-matches-a-pattern" dir="ltr">How to select sub-lists having at least one element that matches a pattern</a> <span class="question-originals-answer-count"> 6 answers </span> </li> </ul> </div> <p>I have a big list, for simple, it just like:</p> <pre><code>list = { {1, Overflow[], 3}, {Overflow[], Overflow[], 7}, {Overflow[], 4, 2}, {7, 8, 9}, {-1, Overflow[], 3}, {-1, 10, 10} }; </code></pre> <p>I don't know where contain <code>Overflow[]</code>, so I want to filter the list by <code>Select[]</code> function, If <code>Overflow[]</code> at the first position, I know it will work by: </p> <pre><code>Select[list, Not[#[] == Overflow[]] &amp;] </code></pre> <p>It will return <code>{{1, Overflow[], 3}, {7, 8, 9}, {-1, Overflow[], 3}, {-1, 10, 10}}</code></p> <p><strong>Is there any way to check all the positions, return <code>{{7, 8, 9}, {-1, 10, 10}}</code>?</strong></p> https://mathematica.stackexchange.com/q/203623 1 extracting pairs in which y-component has a relative maximum value Inzo Babaria https://mathematica.stackexchange.com/users/33967 2019-08-12T08:01:57Z 2019-08-13T07:35:24Z <p>I have a list of several pairs. You can find it <a href="https://xfl.jp/J81IH6" rel="nofollow noreferrer">here</a> or create it manually. Applying <code>ListPlot</code> on the intended list results in the below plot:</p> <p><a href="https://i.stack.imgur.com/u8ftV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u8ftV.png" alt="myplot"></a> </p> <p>How do I extract the maximum pairs <code>(x,y)</code>, whose <code>y</code> components are recognizable in the plot, from the list as an individual list.</p> https://mathematica.stackexchange.com/q/203601 10 How to take the beginning and end parts of a list with simpler syntax? CA Trevillian https://mathematica.stackexchange.com/users/63039 2019-08-11T18:42:05Z 2019-08-12T17:56:03Z <p><strong>So</strong> basically you have a table of some values, let's call it <code>a</code>:</p> <pre><code>a=Table[n,{n,10}]; (*{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}*) </code></pre> <p>And you want to take the beginning and end parts of the table, within the same line, and do <em>something</em> with them. It is my intention to change them all to 0, but really you could change them to some other variable, say <code>b</code>. We want the parts of the list that do not include some inner values, we'll do <code>{4,5,6}</code> which happen to correspond to the indexes 4 through 6, convenient, no?</p> <pre><code>a[[Cases[a, (Except[Alternatives @@ Range[4, 6]])]]] = b; a (*{b, b, b, 4, 5, 6, b, b, b, b}*) </code></pre> <h2><strong>What's a simpler syntax for this? How can we call the end and beginning values of a list in a simpler manner?</strong></h2> <pre><code>a[[Cases[a, (Except[Alternatives @@ Range[4, 6]])]]] (*{1, 2, 3, 7, 8, 9, 10}*) </code></pre> <p>The ideal input would be, to me, something like</p> <pre><code>a[[1;;3;7;;10]] (*{1, 2, 3, 7, 8, 9, 10}*) </code></pre> <p>But this gives</p> <pre><code>(*{7, 8, 9, 10}*) </code></pre> <p>As expected.</p> <h1><strong>How can we simplify the syntax to call the beginning and end parts of a list, leaving out some chosen middle portion?</strong></h1> https://mathematica.stackexchange.com/q/203562 7 How can I iterate this process? geoffrey https://mathematica.stackexchange.com/users/66934 2019-08-10T14:30:33Z 2019-08-10T20:07:45Z <pre><code> a = RandomVariate[UniformDistribution[{0, 1}]]; b = RandomVariate[UniformDistribution[{0, 1}]]; c = RandomVariate[UniformDistribution[{0, 1}]]; a + a b + a b c </code></pre> <p>I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?</p> <p>Best regards Geoffrey Critzer</p> https://mathematica.stackexchange.com/q/203502 5 Arrange a list in ascending order by deleting list elements primefinder https://mathematica.stackexchange.com/users/66915 2019-08-08T22:48:24Z 2019-08-14T09:42:06Z <p>I am new to mathematica and currently learning it by solving some basic problems. </p> <p>I am trying to arrange a list in ascending order using a detailed IF loop. For instance if we start out with <span class="math-container">$\{1,65,40, 155, 120, 122\}$</span>. My output should be <span class="math-container">$\{1,65, 155\}$</span>. However my code yields a different output. Here it is:</p> <pre><code>Sample = {1, 65, 40, 155, 120, 122}; (i = 1; newSample = Sample; Label; newSample = Delete[newSample, i + 1]; If[newSample[[i]] &gt;= newSample[[i + 1]], Goto, i = i + 1 &amp;&amp; i &lt;= Length[newSample]-1 ]; Print[newSample] ) {1,40,155,120,122} </code></pre> <p>I would greatly appreciate any help on why this is happening and how to fix this code. (I would also be interested in seeing some slick ways to write this same code using other(?) built in functions but my main priority is fixing the above code).</p> https://mathematica.stackexchange.com/q/203488 12 Using Select on Dataset with missing keys kickert https://mathematica.stackexchange.com/users/54320 2019-08-08T18:21:24Z 2019-08-14T17:59:49Z <p>I have a very large dataset with about a million entries with up to 75 keys each. Across the whole dataset about half of the keys are blank. In order to reduce filesize, I have used the <a href="https://mathematica.stackexchange.com/questions/181559/deleting-all-keys-that-have-null-values">following code</a> to remove all the blanks keys:</p> <pre><code>DeleteCases[Normal[data], "", {2}] </code></pre> <p>I have another much smaller expression with all the keys pointing to blank strings that I can use with <code>KeyUnion</code> to rebuild the original version if I need to. This works great because it reduces the WL or MX file I am using by about half.</p> <p>My issue comes when I am trying to use <code>Select</code>. The ragged dataset works when all entries have the keys present, but if it missing the key, I get an error.</p> <p>Consider this simplified example:</p> <pre><code>data = Dataset[{&lt;|"Name" -&gt; "Bob", "Location" -&gt; "USA", "Job" -&gt; "Police"|&gt;, &lt;|"Name" -&gt; "Jill", "Location" -&gt; "Canada"|&gt;, &lt;|"Name" -&gt; "Joe", "Location" -&gt; "USA", "Job" -&gt; "Teacher"|&gt;}] </code></pre> <p>When all keys are there, I can use <code>Select</code> as normal:</p> <pre><code>Select[data, #Location == "USA" &amp;] </code></pre> <p>But, if one of the entries is missing the key, I get an error:</p> <pre><code>Select[data, #Job == "Police" &amp;] </code></pre> <p><a href="https://i.stack.imgur.com/VZgrX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VZgrX.png" alt="Error Message"></a></p> <p>There are a few hacky things I can do. I can use KeyUnion to rebuild a full database.</p> <pre><code>keys = &lt;|"Name" -&gt; "", "Location" -&gt; "", "Job" -&gt; ""|&gt; Select[KeyUnion[data, keys], #Job == "Police" &amp;] </code></pre> <p>I can also use <code>KeyExistsQ</code> to do an initial select and then run from there:</p> <pre><code>Select[Select[data, KeyExistsQ["Job"]], #Job == "Police" &amp;] </code></pre> <p>While both of these options yield the results I am looking for, neither are particularly effecient when working with a very large dataset.</p> <p>I found <a href="https://mathematica.stackexchange.com/questions/119588/query-or-mapat-ragged-associations-or-ignoring-missing-values">this question</a> from three years ago that addresses a similar issue with <code>Query</code>, but since that would have been on V10 and I am in V12, I was hoping something had changed to allow <code>Select</code> to work with missing keys.</p> https://mathematica.stackexchange.com/q/203425 1 Creating a tensor of rank $k$ from elements of a list Bobby_Tables https://mathematica.stackexchange.com/users/66898 2019-08-07T14:32:21Z 2019-08-07T16:30:18Z <p>Given a list containing <span class="math-container">$4^k$</span> elements, how do I create a "square" <span class="math-container">$\underbrace{4\times...\times4}_{k\text{-times}}$</span> tensor of rank <span class="math-container">$k$</span>? </p> <p>Intuitively, I would opt for the <code>ArrayReshape</code> function. However, I am unsure how to proceed given the variable number of the tensor rank. </p> <p>In case of <span class="math-container">$k=2$</span>, we would have</p> <pre><code>tensor = ArrayReshape[list, {4,4}] </code></pre> <p>How do I generalize this function to the case of some arbitrary rank <span class="math-container">$k$</span>?</p> https://mathematica.stackexchange.com/q/203417 0 Contour plot of data Johnathan https://mathematica.stackexchange.com/users/66701 2019-08-07T13:04:32Z 2019-08-07T13:13:34Z <p>I have two vectors (plain lists), say <code>a,b</code>, from which I generated a 2d table, say <code>mat</code>. It's not a direct expression like <code>mat[[i,j]] = a[[i]] + b[[j]]</code>, rather some more complicated computation that spans much of my code, but each entry <span class="math-container">$ij$</span> is essentially just some function of the corresponding <code>a[[i]]</code> and <code>b[[j]]]</code>.</p> <p>I'd like to plot that, perhaps as a contour plot, but I see that Mathematica expects the data to be given as a matrix where each entry <span class="math-container">$ij$</span> is a triplet <code>a1[[i]],a2[[j]],mat[[i,j]]</code>.</p> <p>How do I construct this matrix of triplets in a proper Mathematica "way of thinking"? (i.e. without nested loops etc.) Or alternatively, can I use my current data structure and plot it directly?</p> https://mathematica.stackexchange.com/q/203351 0 How to plot this data smoothly AtoZ https://mathematica.stackexchange.com/users/59012 2019-08-06T08:28:09Z 2019-08-06T08:55:25Z <p>I have three lists (appropriately padded), giving me three curves, however the curves are not smooth/continuous and with gaps. I tried to make the grid finer, but still I can not get around this problem. <a href="https://mathematica.stackexchange.com/questions/70102/how-to-connect-discrete-points-and-make-them-become-continuous-curve?noredirect=1&amp;lq=1">This</a> looks like a similar situation but it seems that it doesn't work for my problem. The <code>data</code> is</p> <pre><code>{{2.60012, 2.6001, 2.60004, 2.59994, 2.5998, 2.59962, 2.5994, 2.59914, 2.59885, 2.59851, 2.59813, 2.59771, 2.59725, 2.59675, 2.59621, 2.59563, 2.59501, 2.59435, 2.59365, 2.59291, 2.59213, 2.59131, 2.59045, 2.58954, 2.5886, 2.58761, 2.58658, 2.58551, 2.5844, 2.58324, 2.58204, 2.5808, 2.57951, 2.57817, 2.57679, 2.57537, 2.57389, 2.57237, 2.5708, 2.56918, 2.56751, 2.56579, 2.56402, 2.56219, 2.56031, 2.55837, 2.55637, 2.55431, 2.55219, 2.55, 2.54775, 2.54544, 2.54305, 2.54059, 2.53806, 2.53545, 2.53276, 2.52999, 2.52713, 2.52418, 2.52114, 2.51799, 2.51475, 2.5114, 2.50794, 2.50436, 2.50065, 2.49682, 2.49286, 2.48874, 2.48448, 2.48006, 2.47548, 2.47071, 2.46575, 2.4606, 2.45523, 2.44964, 2.4438, 2.43771, 2.43134, 2.42467, 2.41769, 2.41037, 2.40268, 2.39459, 2.38608, 2.3771, 2.36761, 2.35758, 2.34694, 2.33565, 2.32363, 2.31082, 2.29714, 2.28248, 2.26675, 2.24982, 2.23155, 2.21177, 2.19031, 2.16693, 2.14138, 2.11336, 2.08254, 2.04849, 2.01075, 1.96877, 1.92191, 1.86945, 1.81061, 1.74454, 1.67039, 1.58744, 1.49519, 1.39361, 1.28336, 1.16595, 1.04378, 0.919873, 0.79749, 0.679534, 0.568156, 0.464555, 0.369009, 0.281, 0.199296, 0.121749, 0.04388, 2.74842, 2.74852, 2.74862, 2.74872, 2.74882, 2.74892, 2.74902, 2.74912, 2.74922, 2.74932, 2.74942, 2.74952, 2.74962, 2.74972, 2.74982, 2.74992, 2.75003, 2.75013, 2.75023, 2.75033, 2.75043, 2.75053, 2.75063, 2.75073, 2.75084, 2.75094, 2.75104, 2.75114, 2.75124, 2.75134, 2.75144, 2.75154, 2.75164, 2.75174, 2.75183, 2.75193, 2.75203, 2.75213, 2.75222, 2.75232, 2.75242, 2.75251, 2.75261, 2.7527, 2.7528, 2.75289, 2.75298, 2.75308, 2.75317, 2.75326, 2.75335, 2.75344, 2.75353, 2.75361, 2.7537, 2.75379, 2.75387, 2.75396, 2.75404, 2.75413, 2.75421, 2.75429, 2.75437, 2.75445, 2.75453, 2.75461, 2.75469, 2.75476, 2.75484, 2.75491, 2.75498, 2.75506}, {Null, 2.74159, 2.74159, 2.7416, 2.7416, 2.7416, 2.74161, 2.74161, 2.74162, 2.74163, 2.74163, 2.74164, 2.74165, 2.74166, 2.74167, 2.74169, 2.7417, 2.74171, 2.74173, 2.74174, 2.74176, 2.74178, 2.74179, 2.74181, 2.74183, 2.74185, 2.74187, 2.7419, 2.74192, 2.74194, 2.74197, 2.74199, 2.74202, 2.74205, 2.74207, 2.7421, 2.74213, 2.74216, 2.74219, 2.74223, 2.74226, 2.74229, 2.74233, 2.74236, 2.7424, 2.74244, 2.74248, 2.74251, 2.74255, 2.74259, 2.74264, 2.74268, 2.74272, 2.74277, 2.74281, 2.74286, 2.7429, 2.74295, 2.743, 2.74305, 2.7431, 2.74315, 2.7432, 2.74325, 2.74331, 2.74336, 2.74342, 2.74347, 2.74353, 2.74359, 2.74365, 2.7437, 2.74376, 2.74383, 2.74389, 2.74395, 2.74401, 2.74408, 2.74414, 2.74421, 2.74428, 2.74434, 2.74441, 2.74448, 2.74455, 2.74462, 2.74469, 2.74477, 2.74484, 2.74491, 2.74499, 2.74506, 2.74514, 2.74522, 2.74529, 2.74537, 2.74545, 2.74553, 2.74561, 2.74569, 2.74578, 2.74586, 2.74594, 2.74603, 2.74611, 2.7462, 2.74628, 2.74637, 2.74646, 2.74655, 2.74664, 2.74672, 2.74681, 2.74691, 2.747, 2.74709, 2.74718, 2.74727, 2.74737, 2.74746, 2.74755, 2.74765, 2.74774, 2.74784, 2.74794, 2.74803, 2.74813, 2.74823, 2.74833, 3.0303, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null}, {Null, 2.7416, 2.74163, 2.74168, 2.74174, 2.74183, 2.74193, 2.74206, 2.7422, 2.74236, 2.74254, 2.74273, 2.74295, 2.74318, 2.74343, 2.7437, 2.74399, 2.74429, 2.74461, 2.74494, 2.7453, 2.74567, 2.74605, 2.74645, 2.74687, 2.7473, 2.74775, 2.74821, 2.74869, 2.74918, 2.74969, 2.75021, 2.75074, 2.75129, 2.75185, 2.75243, 2.75301, 2.75362, 2.75423, 2.75486, 2.7555, 2.75615, 2.75681, 2.75749, 2.75818, 2.75888, 2.7596, 2.76032, 2.76106, 2.76181, 2.76258, 2.76335, 2.76414, 2.76494, 2.76575, 2.76658, 2.76741, 2.76826, 2.76913, 2.77, 2.77089, 2.77179, 2.7727, 2.77363, 2.77458, 2.77553, 2.7765, 2.77749, 2.77849, 2.77951, 2.78054, 2.78159, 2.78265, 2.78373, 2.78483, 2.78595, 2.78709, 2.78824, 2.78942, 2.79062, 2.79184, 2.79308, 2.79434, 2.79563, 2.79695, 2.79829, 2.79966, 2.80105, 2.80248, 2.80394, 2.80543, 2.80696, 2.80852, 2.81012, 2.81176, 2.81345, 2.81518, 2.81696, 2.81879, 2.82067, 2.82261, 2.82462, 2.82669, 2.82883, 2.83105, 2.83335, 2.83573, 2.83822, 2.84081, 2.84351, 2.84634, 2.84931, 2.85243, 2.85572, 2.8592, 2.8629, 2.86683, 2.87105, 2.87559, 2.88051, 2.88587, 2.89177, 2.89834, 2.90575, 2.91426, 2.92427, 2.9365, 2.95232, 2.9754, 3.08189, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null}} </code></pre> <p>with</p> <pre><code>ListLinePlot[data, DataRange -&gt; {0, 2}, PlotRange -&gt; {{0, 3}, {0, Pi}}, Frame -&gt; True] </code></pre> https://mathematica.stackexchange.com/q/203349 1 How to sample numerically a function using a mesh-grid function in 3D? irondonio https://mathematica.stackexchange.com/users/62048 2019-08-06T07:44:46Z 2019-08-06T16:22:00Z <p>I am working on a problem where I want to sample a scalar function f[x,y,z].</p> <p>The problem is how to obtain a mesh-grid in 3D (A grid of points in x,y, and z). In order to study this function. </p> <p>Of course, I´ve read previous questions in 2D <a href="https://mathematica.stackexchange.com/questions/30701/simulate-matlabs-meshgrid-function">Simulate MATLAB&#39;s meshgrid function</a>. And also an incomplete answered was reported here <a href="https://mathematica.stackexchange.com/questions/186869/mathematica-implementation-of-meshgrid-in-3d">Mathematica implementation of meshgrid in 3D</a>. </p> <p>Please, How can I solve and fix this code? </p> <pre><code> Clear["Global*"]; meshgrid[x_List, y_List, z_list] := {ConstantArray[x, Length[x]], Transpose@ConstantArray[y, Length[y]], Transpose@ConstantArray[z, Length[z]]} {xx, yy, zz} = meshgrid[Range[-2, 2, .1], Range[-4, 4, .2], Range[-6, 6, .2] ] c = xx*Exp[-xx^2 - yy^2] + 2 zz; pts = Flatten[{xx, yy, zz, c}]; ListPlot3D[pts, PlotRange -&gt; All, AxesLabel -&gt; Automatic, ImagePadding -&gt; 20, Mesh -&gt; 35, InterpolationOrder -&gt; 2, ColorFunction -&gt; "Rainbow", Boxed -&gt; False] </code></pre> <p><strong>The code output is</strong></p> <p>Set::shape: Lists {xx,yy,zz} and meshgrid[{-2.,-1.9,-1.8,-1.7,-1.6,-1.5,-1.4,-1.3,-1.2,-1.1,-1.,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.},{-4.,-3.8,&lt;&lt;38>>,4.},{-6.,-5.8,-5.6,&lt;&lt;45>>,3.6,3.8,&lt;&lt;11>>}] are not the same shape.</p> <p>ListPlot3D: {xx,yy,zz,2.71828^(-1. xx^2-1. yy^2) xx+2. zz} must be a valid array or a list of valid arrays.</p> https://mathematica.stackexchange.com/q/203331 7 Take $n^\text{th}$ sub-list with equal distances aaa https://mathematica.stackexchange.com/users/66878 2019-08-05T23:48:36Z 2019-08-07T10:05:06Z <p>I have been looking at <code>Span</code>, <code>Take</code>, etc. functions but I couldn't figure out how can I extract elements likes this</p> <pre><code>A = {a, b, c, d, e, f, g, h, k, l, m, n, o, p} </code></pre> <p>And I want this : <code>A' = {b, c, f, g, l, m}</code>.</p> <p>Basically, I am looking at the elements with a distance of 4 + their next elements: <code>{b, f, l}</code> + their associated next elements <code>{c, g, m}</code> </p> <p>Thanks!</p> https://mathematica.stackexchange.com/q/203328 2 Stack Two Lists of Depth=2 [duplicate] Histoscienology https://mathematica.stackexchange.com/users/54993 2019-08-05T22:51:03Z 2019-08-06T09:51:28Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/51407/insert-all-elements-from-one-matrix-into-another" dir="ltr">Insert all elements from one matrix into another</a> <span class="question-originals-answer-count"> 6 answers </span> </li> </ul> </div> <p>My question is to combine a matrix of values with a matrix of 2D coordinates:</p> <pre><code>Coor = {{{a, b}, {c, d}}, {{u, v}, {s, t}}}; Value = {{1, 2}, {3, 4}}; </code></pre> <p>I want to get </p> <pre><code>Combined = {{{a, b,1}, {c, d,2}}, {{u, v,3}, {s, t,4}}}; </code></pre> <p>so I can use ListPlot3D</p> <p>This is similar to another question <a href="https://stackoverflow.com/questions/16513010/how-to-combine-two-lists-to-plot-coordinate-pairs?rq=1">here</a>. I attempt to use this solution by doing this</p> <pre><code>MapThread[List, {matrix1, matrix}, 2] </code></pre> <p>But this gives me <code>{{{{a, b},1}, {{c, d},2}}...};</code> which has an extra bracket inside that I couldn't figure out how to remove...</p> <p>How can I get the desired "Combined" list? Thanks!</p> https://mathematica.stackexchange.com/q/203304 1 Calculate the intersection points of a line and a parametrically defined 3D surface William Taylor https://mathematica.stackexchange.com/users/66870 2019-08-05T13:08:54Z 2019-08-05T23:39:07Z <p>Thanks to Henrik Schumacher for advice to simplify the sample code:</p> <p>I would be grateful for any pointers on the following issue.</p> <p>A line from an external point intersects the surface at 2 points (near side and far side) The line can be defined as emanating from the origin {0,0,0} with a horizontal angle L and a vertical angle B (or in Cartesian form) I have tried several approaches to identify the distance along the line for each intersection with the 3D surface. </p> <p>Obviously given any point on the surface this is straight forward and one might think that it would also be straightforward to look for two points on the same line (i.e. same L and B) and thus calculate the distance along the line to each intersection , but this has proved intractable as there are rarely exact matches and more data points creates confusion.</p> <p>I have tried several approaches including meshing but to no avail, they just seem to give another set of points that do not seem to be triangles that share edges, but that may be my misunderstanding of what a mesh does.</p> <p>I note that If there is a single equation rather than a parametric set of them then NSolve or Solve can be used.</p> <p>The example below includes enough code to generate a sample surface the parameters are u and v and the x ,y and z values are given by rather involved functions of which these are simplified examples.</p> <pre><code> ClearAll; fx[i_, j_, v_, h_] := 8.5 - j (1 - Sin[v]) Cos[v] Cos[h]; fy[i_, j_, v_, h_] := i (1 - .2*Sin[v]) Cos[v] Sin[h]; fz[k_, v_] := k (1 - Sin[v]); Manipulate[ FB = ParametricPlot3D[{ fy[i, j, v, h], fx[i, j, v, h], fz[k, v] }, {v, 0, 2 Pi}, {h, -Pi/2, Pi/2}, Mesh -&gt; Automatic ], {{i, 35, "y axis (i)"}, 5, 70, Appearance -&gt; "Labeled"}, {{j, 35, "x axis (j)"}, 5, 70, Appearance -&gt; "Labeled"}, {{k, 50, "z axis (k)"}, 5, 70, Appearance -&gt; "Labeled"} ] </code></pre> <p>The vertices for that above can be generated by</p> <pre><code> fbL = Flatten[Cases[Normal@FB, Line[x_] :&gt; x, Infinity], 1] </code></pre> <p>Here is the original interface that I submitted, I will leave it in the question since there are several problems I had to solve in regard to manipulate and file saving that may be helpful to others.</p> <pre><code> ClearAll; fx [i_, j_, d_, v_, h_, t_, w_, x0_, sf_] := ( i (1 - d *Sin[v]) Cos[v] Sin[h] Sin[t] - j (1 - d*Sin[v]) Cos[v] Cos[h] Cos[t] + w*Log[2 - Sin[v]]*(-Sin[t] + Cos[t]))*sf + x0; fy [i_, j_, d_, v_, h_, t_, w_, y0_, sf_] := (i (1 - d *Sin[v]) Cos[v] Sin[h] Cos[t] + j (1 - d*Sin[v]) Cos[v] Cos[h] Sin[t] + w*Log[2 - Sin[v]]*(Cos[t] + Sin[t]) )*sf + y0; fz[k_, v_, z0_, sf_] := sf* k (1 - Sin[v])/2 + z0; fR[x_, y_, z_] := Sqrt[x^2 + y^2 + z^2]; fL[x_, y_] := ArcTan[y/x]; fB[x_, y_, z_] := ArcSin[z/Sqrt[x^2 + y^2 + z^2]]; buildXYZCoords[np_, nq_, i_, j_, k_, d_, t_, w_, basex_] := Module[{arr}, pi = 3.14159265358979; hstep = pi/(nq - 1); vstep = 2*hstep; mb = fz[k, 0, 0, 1]; sf = basex * Tan[d2r[mb]]/ mb; arr = {}; For[v = 0, v &lt;= 2 * pi, v = v + vstep, For[h = -pi/2, h &lt;= pi/2, h = h + hstep, x = fx[i, j, d, v, h, t, w, basex, sf]; y = fy[i, j, d, v, h, t, w, 0, sf]; z = fz[k, v, 0, sf]; arr = Join[arr, {{x, y, z}}]; ]; ]; (* the last join must NOT have a semicolon, a NULL return coccurs if it does, hence the line below *) arr = Join[arr, {}] ] buildCoords[np_, nq_, i_, j_, k_, d_, t_, w_, basex_] := Module[{arr}, pi = N[Pi]; hstep = pi/(nq - 1); vstep = 2*hstep; mb = fz[k, 0, 0, 1]; sf = basex * Tan[d2r[mb]]/ mb; arr = {{"id", "mid", "h", "v", "x", "y", "z", "R", "L", "B", "Ldeg", "Bdeg", "xm", "ym", "zm", "Rm", "Lm", "Bm", "Ldegm", "Bdegm"}}; vid = 0; mid = 0; For[v = 0, v &lt;= 2 * pi, v = v + vstep, For[h = -pi/2, h &lt;= pi/2, h = h + hstep, x = fx[i, j, d, v, h, t, w, basex, sf]; y = fy[i, j, d, v, h, t, w, 0, sf]; z = fz[k, v, 0, sf]; R = fR[x, y, z]; L = fL[x, y]; B = fB[x, y, z]; dL = r2d[L]; dB = r2d[B]; vid = vid + 1; arr = Join[arr, {{vid, mid, h, v, x, y, z, R, L, B, dL, dB}}]; ]; ]; (* for export Semicolons as used above are required but the last \ join must NOT have a semicolon i.e. output not surpressed hence the line below *) arr = Join[arr, {}] ] r2d[r_] := N[r*180/Pi]; d2r[d_] := N[d *Pi/180]; m8 = Manipulate[ FB = ParametricPlot3D[{ fy [i, j, d, v, h, t, w, 0, 1], fx [i, j, d, v, h, t, w, k, 1], fz[k, v, 0, 1]}, {v, 0, 2 π}, {h, -π/2 , π/2}, ViewPoint -&gt; Dynamic@vp, Mesh -&gt; Automatic, MeshStyle -&gt; mcolor, PlotStyle -&gt; scolor, AspectRatio -&gt; 1, PlotRange -&gt; {{-50, 50}, {0, 150}, {0, 100}}, AxesLabel -&gt; {"y", "x", "z"} ], "Parameters defining the surface shape", {{i, 35, "L ~y axis (i)"}, 5, 70, Appearance -&gt; "Labeled"}, {{j, 35, "To GC ~x axis (j)"}, 5, 70, Appearance -&gt; "Labeled"}, {{k, 50, "B ~z axis (k)" }, 5, 70, Appearance -&gt; "Labeled"}, {{d, 0.2, "Thinness (d)"}, 0, 1, Appearance -&gt; "Labeled"}, {{t, 0, "xy Tilt (t)"}, -Pi/2, Pi/2, Appearance -&gt; "Labeled"}, {{w, 0, "'Wind' factor (w)"}, -20, 20, Appearance -&gt; "Labeled"}, Delimiter, {{vp, {0, 8.5, 0}, "View Point"}, {{8.5, 8.5, 8.5} -&gt; "xyz", {0, 8.5, 0} -&gt; "yz", {0, 0, 8.5} -&gt; "yx", {8.5, 0, 0} -&gt; "xz"}, PopupMenu, ImageSize -&gt; 140}, Row[{Spacer, Button["Reset", FrontEndExecute[FrontEndToken["EvaluateNotebook"]], ImageSize -&gt; 140]}], {{mcolor, RGBColor[0., 1., 0.07], "Mesh"}, ColorSlider}, {{scolor, RGBColor[255, 72, 255], "Surface"}, ColorSlider}, Delimiter, Row[ {Spacer, Button["Export cartessian vertices", Export[ SystemDialogInput["FileSave", ToString[ StringForm["FBdata_i=_j=_k=_d=_t=_w=.xlsx", i, j, k, d, t, w]]], TableForm[FB[[1, 1, 1]], TableHeadings -&gt; {None, {"x", "y", "z"}} ]] , Method -&gt; "Queued", ImageSize -&gt; 160]}], Row[{Spacer, Button["Export to STL", Export[ SystemDialogInput["FileSave", ToString[ StringForm["FBdata_i=_j=_k=_d=_t=_w=.stl", i, j, k, d, t, w]]], FB] , Method -&gt; "Queued", ImageSize -&gt; 160]}], Delimiter, "Export coordinates x,y,z,L,B,R with a specified density of data \ points", "Distance of bubble base from observer", {{basex, 8.5, "Observer to Base: basex (Kpc)"}, 4, 10, Appearance -&gt; "Labeled"}, {{ed, 60, "Export density"}, 10, 1000, Appearance -&gt; "Labeled"}, Row[{Spacer, Button["Print Parameters", Print["buildCoords[" &lt;&gt; ToString[ed] &lt;&gt; "," &lt;&gt; ToString[ed] &lt;&gt; "," &lt;&gt; ToString[i] &lt;&gt; "," &lt;&gt; ToString[j] &lt;&gt; "," &lt;&gt; ToString[k] &lt;&gt; "," &lt;&gt; ToString[d] &lt;&gt; "," &lt;&gt; ToString[t] &lt;&gt; "," &lt;&gt; ToString[w] &lt;&gt; "," &lt;&gt; ToString[basex] &lt;&gt; "] // TableForm"], Method -&gt; "Queued", ImageSize -&gt; 160]}], Row[{Spacer, Button["Export Data", Export[SystemDialogInput["FileSave", ToString[ StringForm[ "FBdata_np=_i=_j=_k=_d=_t=_w=_gcd=__full_\ with_L_B.xlsx", ed, i, j, k, d, t, w, basex]]], Evaluate@buildCoords[ed, ed, i, j, k, d, t, w, basex]], Method -&gt; "Queued", ImageSize -&gt; 160]}], Row[{Spacer, Button["Print Data", Print[TableForm[ Evaluate@buildCoords[ed, ed, i, j, k, d, t, w, basex]]], Method -&gt; "Queued", ImageSize -&gt; 160]}], Row[{Spacer, Button["Print Calculated XYZ", Print[TableForm[ Evaluate@buildXYZCoords[ed, ed, i, j, k, d, t, w, basex]]], Method -&gt; "Queued", ImageSize -&gt; 160]}], Row[{Spacer, Button["Plot Calculated Coords", Print[ListSurfacePlot3D[ buildXYZCoords[ed, ed, i, j, k, d, t, w, basex], PlotRange -&gt; {{0, 20}, {-10, 10}, {0, 20}}, AxesLabel -&gt; {"x", "y", "z"}, PlotLabel -&gt; {"Earth at (0,0,0)"}]], Method -&gt; "Queued", ImageSize -&gt; 160]}] ] </code></pre> https://mathematica.stackexchange.com/q/203267 1 How to making a repeting list out of tables? user66861 https://mathematica.stackexchange.com/users/66861 2019-08-04T20:13:42Z 2019-08-05T08:43:45Z <p>I'm a little bit trouble with the below task, I'd like to make a repeting list out of a table. This is the starting point for the list</p> <p><a href="https://i.stack.imgur.com/Q0KNM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q0KNM.png" alt="enter image description here"></a></p> <p>And I like to make them into this form: <a href="https://i.stack.imgur.com/j7JgR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j7JgR.png" alt="enter image description here"></a></p> <p>How should I make this? Thank you very much for the answers in advance too!</p> https://mathematica.stackexchange.com/q/203203 13 Map vs. Table for index-specific operations on 2D arrays theorist https://mathematica.stackexchange.com/users/45215 2019-08-03T00:47:50Z 2019-08-05T07:51:57Z <p>Many experienced users on this site tend to use Map (and its variants, MapAt, MapIndexed, etc.) rather than Table. When applying the same operation to every element of an array, Map does seem more semantically direct. For instance:</p> <pre><code>test2D = {{a1, a2, a3}, {b1, b2}, {c1, c2, c3, c4}}; Table[g[test2D[[row, col]]] + 1, {row, 1, Length@test2D}, {col, 1, Length@test2D[[row]]}]; MatrixForm[%, TableAlignments -&gt; Left] Map[g[#] + 1 &amp;, test2D, {2}]; MatrixForm[%, TableAlignments -&gt; Left] </code></pre> <p>But when I need to carry out index-specific (i.e., position-specific) operations on higher-dimensional(>=2D) arrays, I find Map and its variants more challenging than Table.</p> <p>For instance, suppose I want to raise each element in a 1D array to a power equal to its position. That I can do with either Table or MapIndexed:</p> <pre><code>test1D = {a1, a2, a3}; Table[test1D[[col]]^col, {col, 1, Length@test1D}] Flatten[MapIndexed[#1^#2 &amp;, test1D], 1] </code></pre> <p>But suppose I want to raise each element in a 2D array to a power equal to its row no. x column no. With Table that's conceptually straightforward:</p> <pre><code>Table[test2D[[row, col]]^(row*col), {row, 1, Length@test2D}, {col, 1, Length@test2D[[row]]}] </code></pre> <p>But how would one do that with MapIndexed? It would be nice if it were just something like:</p> <pre><code>MapIndexed[#1^(#2*#3) &amp;, test2D] </code></pre> <p>where #2 were the column index and #3 were the row index, but it doesn't work like that. </p> <p>Finally, suppose you have more detailed index-specific operations in a 2D array. That seems to be where Table really shines, but I'd be interested to hear of alternatives. E.g., suppose that, from each successive 4-element block of data in a row, you need to extract the 2nd and 4th elements, but only when all four elements are present. Thus, in a row of {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10}, you need {{a2, a4}, {a6, a8}}. And you need to do this for each successive row. Further, the rows have variable lengths. With Table, this does the job:</p> <pre><code>test2Dx = {{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20}, {b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11}, {c1, c2, c3, c4, c5, c6, c7}, {d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17}}; Table[{test2Dx[[row, 2 + col*4]], test2Dx[[row, 4 + col*4]]}, {row, 1, Length@test2Dx}, {col, 0, (Floor[N[Length[test2Dx[[row]]]/4]]) - 1}]; MatrixForm[%, TableAlignments -&gt; Left] </code></pre> <p>Is there a semantically straightforward way to do this using other functions (e.g., Map or its variants and a pure function)—or is this a use case for which Table makes more sense?</p> https://mathematica.stackexchange.com/q/203174 8 Return last number in sub-sequences in a list of integers Jonathan Kinlay https://mathematica.stackexchange.com/users/36994 2019-08-02T13:09:49Z 2019-08-02T18:05:12Z <p>Given a list of integers like this:</p> <pre><code>z = {113, 117, 118, 119, 120, 121, 475, 476, 529, 538, 542, 543, 544} </code></pre> <p>I want a function that returns:</p> <pre><code>{113, 121, 476, 529, 538, 544} </code></pre> <p>here's the pedestrian version:</p> <pre><code>LastinSequence[a_] := Module[ {n}, n = {}; Do[ If[ a[[i]] &gt; (1 + a[[i - 1]]), n = Append[n, a[[i - 1]]] ]; , {i, 2, Length@a} ]; n = Append[n, Last@a]; n ] </code></pre> <p>Horrible.</p> <p><strong>Can I have some suggestions to do this elegantly (and quickly!) in Wolfram language?</strong></p> <p>Also, a variation <code>FirstinSequence[a_]</code> that would return:</p> <pre><code>{113 117, 475, 529, 538, 542} </code></pre> <p>Thanks.</p> https://mathematica.stackexchange.com/q/203149 2 Confused how transpose works for lists acoustics https://mathematica.stackexchange.com/users/53981 2019-08-02T07:05:32Z 2019-08-02T07:18:13Z <p>I have two lists and I want to find the value of MAC using below formula<a href="https://i.stack.imgur.com/OBNIf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OBNIf.png" alt="formula"></a>. But the problem is transpose on the list is not permissible in Mathematica?. If I just carry out without transpose, I am getting MAC value as 1. which is wrong. I am expecting a MAC value which is between to 0.9-0.95. And the other question, Is Transpose function only applies for matrices? </p> <pre><code>Subscript[ϕ, a] = {0., 0.00758684, 0.0151222, 0.0225548, 0.0298332, 0.036906, 0.0437221, 0.0502304, 0.0563798, 0.0621194, 0.0673986, 0.0721668, 0.0763735, 0.0799688, 0.0829025, 0.085125, 0.0865867, 0.0872383, 0.0870309, 0.0859154, 0.0838435, 0.0807667, 0.076637, 0.0714063, 0.0650272, 0.057452, 0.0486335, 0.0385245, 0.027078, 0.0142472, -0.0000147872, -0.0207673, -0.043783, -0.0688821, \ -0.095885, -0.124613, -0.154887, -0.18653, -0.219366, -0.25322, \ -0.287917, -0.323288, -0.359162, -0.395372, -0.431753, -0.468145, \ -0.504389, -0.540329, -0.575814, -0.610698, -0.644836, -0.67809, \ -0.710325, -0.741414, -0.771232, -0.799661, -0.826587, -0.851904, \ -0.875511, -0.897313, -0.917222, -0.935156, -0.95104, -0.964807, \ -0.976394, -0.985748, -0.992822, -0.997576, -0.999977, -1., \ -0.997627, -0.992848, -0.98566, -0.976065, -0.964077, -0.949712, \ -0.932996, -0.913962, -0.892648, -0.869101, -0.843373, -0.815522, \ -0.785615, -0.75372, -0.719916, -0.684284, -0.646911, -0.607891, \ -0.567319, -0.525299, -0.481935, -0.437337, -0.391619, -0.344896, \ -0.297288, -0.248917, -0.199906, -0.150381, -0.100469, -0.0502993, 0.}; Subscript[ϕ, x] = {8.61215*10^-6, 0.00907824, 0.0180589, 0.0268619, 0.0353983, 0.0435798, 0.0513177, 0.0585241, 0.0651109, 0.0709904, 0.0760754, 0.0802789, 0.0835142, 0.0856953, 0.0867361, 0.0865515, 0.0850563, 0.0821661, 0.0777966, 0.0718642, 0.0642853, 0.054977, 0.0438563, 0.0308408, 0.015848, -0.00120439, -0.02042, -0.0415716, -0.0645701, \ -0.0892686, -0.11552, -0.143179, -0.1721, -0.202138, -0.23315, \ -0.264993, -0.297527, -0.330612, -0.36411, -0.397886, -0.431805, \ -0.465736, -0.49955, -0.53312, -0.566323, -0.599037, -0.631146, \ -0.662535, -0.693093, -0.722713, -0.751293, -0.778732, -0.804937, \ -0.829815, -0.853282, -0.875254, -0.895657, -0.914417, -0.931468, \ -0.946747, -0.960198, -0.971769, -0.981414, -0.989092, -0.994769, \ -0.998413, -1., -0.999512, -0.996936, -0.992263, -0.985491, \ -0.976624, -0.965671, -0.952645, -0.937566, -0.920459, -0.901355, \ -0.880288, -0.857298, -0.832431, -0.805737, -0.777269, -0.747088, \ -0.715256, -0.68184, -0.646913, -0.610549, -0.572828, -0.533831, \ -0.493644, -0.452356, -0.410057, -0.366842, -0.322806, -0.278048, \ -0.232667, -0.186765, -0.140444, -0.0938079, -0.0469617, \ -0.0000101546} mac = (Abs[ Subscript[ϕ, a].Subscript[ϕ, x]])^2/((Subscript[ϕ, a].Subscript[ϕ, x]) (Subscript[ϕ, x].Subscript[ϕ, a])) </code></pre> https://mathematica.stackexchange.com/q/203114 1 Rotating points on a plane Vaggelis_Z https://mathematica.stackexchange.com/users/5052 2019-08-01T16:41:09Z 2019-08-02T02:13:49Z <p>If we have a pair of coordinates <span class="math-container">$(x,y)$</span>, let's say</p> <pre><code>pt = {1,2} </code></pre> <p>then we can easily rotate the coordinates, by an angle <span class="math-container">$\theta$</span>, by using the rotation matrix</p> <pre><code>R = {{Cos[\[Theta]], -Sin[\[Theta]]}, {Sin[\[Theta]], Cos[\[Theta]]}}; </code></pre> <p>as</p> <pre><code>pt2 = pt.R; </code></pre> <p>Now let's assume that we have a collection of points in the form</p> <pre><code>data = {{1}, {-0.3, 1}, {2, -0.2}, {2}, {-2, 1}, {4,-2}, {3}, {1, 1}, {-0.2, -0.3}} </code></pre> <p>where the integers 1, 2 and 3 count the subsets of the list <code>data</code>. </p> <p>My question: how can we rotate the <span class="math-container">$(x,y)$</span> coordinates of the list <code>data</code> by and angle, let's say <span class="math-container">$2\pi/3$</span> and create a new list, <code>data2</code> of the form</p> <pre><code>data2 = {{1}, {rotated x, rotated y}, {rotated x, rotated y}, {2}, {rotated x, rotated y}, {roatetd x, rotated y}, ...} </code></pre> <p>Any suggestions?</p>