Tag Info

Hot answers tagged

11

I did a direct translation from Matlab file logspace.m with additional Mathematica minor touches. Mathematica Function logspace[d1_?(Element[#, Reals] &), dd2_?(Element[#, Reals] &), n_?(IntegerQ[#] && # > 0 &)] := Module[{d2 = dd2, i}, If[d2 == Pi, d2 = Log[10, d2]]; Flatten@{Table[10^(d1 + ...


11

tbl = RandomInteger[{0, 3}, {10, 2}] (* {{0, 3}, {0, 0}, {1, 3}, {2, 0}, {2, 0}, {0, 0}, {1, 1}, {2, 2}, {1, 0}, {3, 3}}*) You have many alternative methods: Cases[tbl, {x_, 0} :> x] (* or *) Cases[tbl, {_, 0}][[All, 1]] (* or *) DeleteCases[tbl, {_, Except[0]}][[All, 1]] (* or *) Select[tbl, Last[#] == 0 &][[All, 1]] (* or *) ...


9

It seems that this answer is proving popular. Let me take my suggestion to the next level. Here is a function findBadSets that will find any explicitly bad Set/SetDelayed attempts in a given expression. Simply wrap it around a syntactically complete block of code, or follow the block with // findBadSets and the errors are printed one per row, protected ...


9

First, you should not start user Symbol names with capital letters as these can easily conflict with internal system functions. There are surely quite a few ways of doing this. It is not clear if you value performance over clarity, etc. Replace by pattern Ignoring the regularity of the data (and applicable to cases where it is not) you could use ...


9

You can also do it this way: {{1, 2}, {2, 4}, {2, 8}} /. {x_, y_} -> {x, 2 y} Which gives: {{1, 4}, {2, 8}, {2, 16}} You can change 2 in 2 y to whatever constant you want. This method is flexible because, say you want to multiply the first dimension by a different constant you just put that number in front of x. E.g. Suppose you want to multiply the ...


8

The lightly-documented second argument of Return works: Table[ If[i < 3, Print@i, Return["Exit", Table]], {i, 100} ] 1 2 "Exit" Other examples of use: (1), (2) Additional information: What can be used as the second argument to Return on your own functions? Break also accepts a second argument but at least on my system the syntax ...


8

I don't belive there is a build in function for this, however you can easily do it using Range fSpace[min_, max_, steps_, f_: Log] := InverseFunction[f] /@ Range[f@min, f@max, (f@max - f@min)/(steps - 1)] Inverse functions are being used so it'll give warnings in cases where you should be cautius, however it works for Log and other invertible ...


8

I'd probably use Sow[]/Reap[] for this case, along with Do[]: With[{y = 0.1}, Reap[Do[ If[And @@ Positive[temp = {10 - x - y^2, 1/x - y}], Sow[temp], Break[]], {x, 0.1, 15, 0.5}]][[-1, 1]]] which yields {{9.89, 9.9}, {9.39, 1.56667}, {8.89, 0.809091}, {8.39, 0.525}, {7.89, 0.37619}, ...


7

t1 = Table[{x, y, 0.}, {x, 0, 5}, {y, 0, 5}] (* {{{0, 0, 0.}, {0, 1, 0.}, {0, 2, 0.}, {0, 3, 0.}, {0, 4, 0.}, {0, 5, 0.}}, {{1, 0, 0.}, {1, 1, 0.}, {1, 2, 0.}, {1, 3, 0.}, {1, 4, 0.}, {1, 5, 0.}}, {{2, 0, 0.}, {2, 1, 0.}, {2, 2, 0.}, {2, 3, 0.}, {2, 4, 0.}, {2, 5, 0.}}, {{3, 0, 0.}, {3, 1, 0.}, {3, 2, 0.}, {3, 3, 0.}, {3, 4, 0.}, {3, 5, ...


7

testList = Table[Import["C:\\txtFiles\\txtFile_" <> ToString[i] <> ".txt", "Data"], {i, 4}] Or, you can map over the files you want to import. Say you have them in the directory where your notebook is saved. files = FileNames[NotebookDirectory[] <> "*.txt"]; testList = Import[#, "Data"] & /@ files Both times you will get the ...


7

The files don't need to be named nicely, either. Say you have a list of filenames: allNames = {"filename.txt", "thatFile.txt", "thisfile.txt", ... }; Now you can read them all in: allData = Table[Import[allNames[[i]],"Data"],{i, 1, Length[allNames]}]; It is not even necessary to use an integer iterator, as in: allData = Table[Import[name, "Data"], ...


7

You can pull out the points by searching for Polygon objects in addHatToL /@ Take[Flatten[Take[LsOnDodecahedron, All]], All]: p1 = addHatToL/@Take[Flatten[Take[LsOnDodecahedron, All]], All]; surfacepoints = Flatten[Flatten[Cases[Flatten[p1[[#]]],Polygon[m_] ->m], 1]&/@Range[Length[p1]], 1]; surfacepoints will then give you the points on the surface ...


6

I'm not sure what you're trying to do here and most probably Nasser's admonition rings true, but perhaps you want something like this?: Do[ CellPrint @ Cell[BoxData[RowBox[{RowBox[{"r", "[", i, "]"}], "="}]], "Input"], {i, 5} ] Alternatively you might make use of \[Placeholder] and Defer: Do[CellPrint @ ExpressionCell[Defer[r[#] = \[Placeholder]], ...


6

Your problems mostly stem from wrongly scoped variables and not understanding what NDSolve returns. I found it easier to generate the pendulum graphics in a Table expression that does not wrap the Graphics expression. Also, time t must be introduced into the Manipulate for it show the pendulums moving in time. With[{time = 10., g = 9.8}, Manipulate[ ...


6

Something like Pick @@ Transpose@largetable~Join~{0} might do it. Unless 0 should be the Real number 0. If you have both, then try Pick @@ Transpose@largetable~Join~{0 | 0.} Edit: The above is the same as Apply[Pick, Join[Transpose[largetable], {0 | 0.}]] and has the same effect as With[{columns = Transpose[largetable]}, Pick[columns[[1]], ...


6

You have to sacrifice something, but it depends on your preferences what you want to keep and what you want to give up. Let's assume you don't want to sacrifice being able to use assignments such as w=3, then you may have to give up using {...} as a wrapper grouping the names of the variables together. You could then define a new wrapper myList to be used ...


6

It may be helpful to understand the cause of the problem. Table acts much like Block in the way that it applies the values of its variables. Because of this it will affect things beyond explicit appearances of the variable in the body of Table. NDSolve, despite having syntax highlighting that indicates that x is localized, does in fact not have the ...


6

Generic short-circuiting Table Preamble I interpret your question as a general request for an implementation of Table that would, while supporting the general syntax of Table, support the short-circuiting as well. While the core of the implementation described below uses Reap and Sow, similarly to other answers, the advantage of the present approach is its ...


5

It seems Mathematica does not like subscript variables for ParallelTable So if you define r1 = Abs[x + y I]; r2 = Abs[x + y I - a]; then plots = ParallelTable[ ContourPlot[{Abs[r1]/Abs[r2] == c1, c2 == Abs[r2] - Abs[r1]} /. a -> 5 // Evaluate, {x, -6, 6}, {y, -6, 6}, FrameTicks -> {Range[-6, 6, 1], Range[-6, 6, 1], None, None}, ...


5

If you select your output cell (by the bracket on the right), it can be converted to bitmap via the Cell $\rightarrow$ Convert To $\rightarrow$ Bitmap menu option. For programmatic conversion: If you prefer bitmaps, you can rasterize your table: table = TableForm[{{5, 7}, {4, 2}, {10, 3}}, TableHeadings -> {{"A", "B", "C"}, {"1", "2"}}]; ...


5

I wasn't sure exactly what terms were included in your summations - here are some ideas though: Define h[1] = Table[{Cos[n*Pi/3], Sin[n*Pi/3]}, {n, 0, 5, 1}]; as you have. Then try h[k_] := Plus @@@ Tuples[h[1], k] if the sums you are referring to can include the same element multiple times, or try h[k_] := Plus @@@ Subsets[h[1], {k}]if you're summing over ...


5

Ok, here is the code. Made small changes at the very end. I am sure there is a more elegant way to do it. But I simply added the counter column at the left using Insert[] and added the "=" using a Map. That is pretty much it. Changes: changed your listaffine line to remove "=" that you had there. Partition it to 4 column now. use Map to format it with ...


5

In version 7 you will need to use use DistributeDefinitions for FftShift1D and eqn. Furthermore, as Szabolcs kindly explains in the comments you should be using DistributeDefinitions for the other symbols as well, instead of SetSharedVariable: DistributeDefinitions[FftShift1D, eqn, kxmin, kxmax, kxdiv, kxinc, zmin, zmax, zdiv, zinc, kspace]; With this ...


5

The short answer is "don't use a For loop". The longer answer is: you can do this using Table and you definitely don't need to pre-populate testlist. Instead do something like this: testlist = Table[Import["C://txtfile_" <> ToString[i] <> ".txt", "Data"], {i, 4}] You can then access the imported files as testlist[[1]] and so on. Asides ...


5

Here is something not far from your example: Collecting data using Sow and Reap as shown by cormullion: Sow[{i, a, b, La, Mu, Row@{tthetaLa, tLacalc}, Row@{tthetaMu, tMucalc}}] We get: dat = {{1, -3, 5, 0.0557281, 1.94427, Row[{0.114562, "*"}], Row[{7.66874, "*"}]}, {2, -3, 1.94427, -1.11146, 0.0557281, Row[{-0.987578, "*"}], Row[{0.114562, " "}]}, ...


5

The following will take all of the data that you want from the json file and give you a Table that you can use as you wish. Is this what was wanted? elementList = {"Sc" -> 1, "Ti" -> 2, "V" -> 3, "Cr" -> 4, "Mn" -> 5, "Fe" -> 6, "Co" -> 7, "Ni" -> 8, "Cu" -> 9, "Zn" -> 10, "Y" -> 11, "Zr" -> 12, "Nb" -> 13, "Mo" ...


4

I'm guessing the OP is looking a way to generate a number of indexed assignments from a list of data elements. If so, then maybe something like the following will work for the OP: Clear[data, r]; data = RandomInteger[{0, 99}, 5]; MapIndexed[(r[#2[[1]]] = #) &, data]; ?r Global`r r[1]=14 r[2]=95 r[3]=39 r[4]=26 r[5]=60


4

Not a complete solution but a few comments too detailed for a comment. a) Firstly make use of listability. Coinicdentally mentioned this the other day as well. It is important because listable functions thread themselves athrough lists -- for want of a better description -- and as a rule perform their operations on lists much faster than comparable use of ...


4

p = Plot[Sin[x], {x, -Pi, Pi}, ImageSize -> 200] size = ImageSize /. FullOptions[p]; Table[Plot[Sin[x], {x, -Pi, Pi}, ImageSize -> size], {6}]


4

Whenever you can, you should be using Map[] instead of tables: k =FindMaximum[y@t/.NDSolve[{y''[t] == -10 - 3/10 Sin@#, y'[0] == 12 Sin@#, y[0] == 0}, y[t], {t, 0, 2 Pi}], {t, 2}] & /@ (Pi/180 Range[10, 80]) Now, plotting the max vs t: ListPlot[{t /. #[[2]], #[[1]]} & /@ k] Now, the max vs n: ListPlot[k[[All, ...



Only top voted, non community-wiki answers of a minimum length are eligible