Tag Info

Hot answers tagged

16

Here's a somewhat simpler way: logspace[a_, b_, n_] := 10.0^Range[a, b, (b - a)/(n - 1)] This gives a sequence starting at 10^a and ending at 10^b, with n points logarithmically spaced, as does MATLAB's logspace() function.


12

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 ...


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 *) ...


11

Try this: n = 1000; coeffs = RandomVariate[NormalDistribution[], n]; f[x_] := Sum[coeffs[[k]] Sin[k x]/k, {k, 1, n}]; Plot[Evaluate@f[x], {x, 0, 2. Pi}, PlotPoints -> n, MaxRecursion -> 0, Mesh -> All] // Timing With[{n = 1000}, First@Timing[Table[Evaluate@f[x], {x, 0, 2. Pi, 2. Pi/n}]] ] 2 times as fast as plot. I remembered my own ...


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

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}, ...


9

This one is fairly fast. I used GatherBy to collect like data rows and kept the ones that matched another. (I assumed that the id entries of a and b are unique in each table.) The appropriate entries are then extracted. On 10000/5000 entries: a = Table[{x, RandomReal[]}, {x, 1, 10000}]; b = Table[{x, RandomReal[]}, {x, 1, 10000, 2}]; ...


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

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 ...


8

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 ...


7

The Problem I believe this is a bug in TableForm. We can see by looking at the Box form of the output that the option ColumnAlignments of the outermost GridBox does not behave as it should. tab = {{1, {1, 1, 1}}, {2, {2, 2, 2}}, {3, {3, 3, 3}}, {4, {4, 4, 4}}}; getOption = Options[First@ToBoxes@#, ColumnAlignments] &; tForm = TableForm[tab, ...


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 ...


7

Cases[{{1, 2}, {2, 4}, {2, 8}}, {x_, y_} -> {x, 2 y}] Table[{First@i, Last@i*2}, {i, {{1, 2}, {2, 4}, {2, 8}}}] {#1, 2 #2} & @@ Transpose@{{1, 2}, {2, 4}, {2, 8}} // Transpose data = RandomReal[10, {10^6, 2}]; r1 = {#1, 2 #2} & @@ Transpose@data // Transpose; // Timing r2 = {#1, 2 #2} & @@@ data; // Timing r1 == r2 (* {0.109201, Null} ...


7

You can use ArrayPlot or MatrixPlot. Note that Mesh setting is optional. It works well if squares are large, but should not be used for small square sizes. MatrixPlot is very intelligent for large arrays of data - it deduces best approximate visual form: "sufficiently large or sparse matrices are downsampled so that their structure is visible in the plot" ~ ...


7

Why not use Clip? MyList = RandomReal[{-1, 1}, 10]; c = {0, 0}; t = 1; r = -1; Clip[ Table[EuclideanDistance[MyList[[i]], c], {i, 1, Length[MyList]}], {0, 1}, {0, r}] (* ==> {0.9957995322104205`,0.3452581732209688`,0.016464628727136405`, -1,-1,0.5914902487316964`,0.8531216593853862`,-1,0.9996775567985703`, -1} *) Here, r = -1 is the value that ...


7

If it is only for display purposes, you could wrap the unwanted elements in Invisible which works pretty well, because the invisible part will have the same size as the element you wrapped. The rules to create the numbers is pretty easy, but the empty elements are not consistent. I see it has something to do with the distance to the {0,0} element, but only ...


7

The iterator variable in the package lives in the context that your package sets (in this case, mypackage`Private`), but the call to ParallelTable from inside your package does not distribute the definitions in your package, because the parallel functions only distribute contexts that are listed in $DistributedContexts. This is by default set to $Context, ...


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

For some reason, your nested data rows throw off the alignment. If you Column your second entries, you get this: table[[All, 2]] = Column /@ table[[All, 2]]; TableForm[table, TableHeadings -> {None, {"Dihedral", "\[Tau] per state [ns]"}}, TableAlignments -> {Left}] Personally, I mostly switched over to Grid because it is more versatile, but of ...


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

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"}}]; ...


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

I recommend you create a List of lists (which is how you represent an array or matrix in Mathematica): mytable = Prepend[data, label] (* {{"x", "y", "z"}, {0.15339, 0.622021, 0.932763}, {0.804435, 0.894563, 0.0611165}, {0.786724, 0.980867, 0.766825}} *) Then Export it to an Excel file: Export["mytable.xls", mytable] And then import it into Word, which ...


6

You can use Word's "Convert Text to Table" function for this. Select the grid in Mathematica and copy as plain text. Paste it into Word, select it and convert to a table You get this:


6

Why don't you use the CIE color matching functions to turn the sampled spectrum contained in each pixel into a color as you would perceive it yourself? Lots of color matching functions here. Let's import the old but much used 1931 dataset: cie = Import["http://cvrl.ioo.ucl.ac.uk/database/data/cmfs/ciexyz31_1.csv"]; They look like this: {cie[[All, 1]], ...



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