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.


15

arr = {{1, 2}, {2, 4}, {2, 8}}; arr[[All,2]] *= 2;


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


11

Here is a start (adjusted based on Szabolcs's statement that Row and TeXForm don't work together in v9): Clear[n] # == ReleaseHold[#] & /@ Array[HoldForm[Sum[i^#, {i, 1, n}]] &, 5] // Column // TeXForm $\begin{array}{l} \sum _{i=1}^n i^1=\frac{1}{2} n (n+1) \\ \sum _{i=1}^n i^2=\frac{1}{6} n (n+1) (2 n+1) \\ \sum _{i=1}^n i^3=\frac{1}{4} ...


11

Here's a way that I find clear: Make sure i and n have no value: i=.; n=.. Generate a list of the held sum expressions, taking care to make the exponent go away from $i^1$. list = Table[With[{e = i^k}, HoldForm[Sum[e, {i, 1, n}]]], {k, 5}] Use ReleaseHold to make a table of results: TeXForm@TableForm[# == ReleaseHold[#] & /@ list] $$ ...


10

Works nicely: {{1, 2}, {2, 4}, {2, 8}}.DiagonalMatrix[{1, 2}] DiagonalMatrix[] is particularly convenient for scaling rows or columns. From whuber: Multiplying by a diagonal matrix is fast for up to somewhere between 100 and 1000 columns; beyond that, a solution modeled after Transpose[{1, 2} * Transpose[a]] becomes superior. Even better for such ...


10

I like to use Apply for this kind of task. {#1, 2 #2} & @@@ {{1, 2}, {2, 4}, {2, 8}}


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

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


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


9

Perhaps something like the following. Not very elegant but it should get the job done. Generate some data for demonstration purposes... SeedRandom[125]; data = Sort@RandomChoice[{1, 2}, {10, 3}] Determine how many replicates there are. reps = Join @@ Replace[Split[data], x_ :> ConstantArray[Length[x], Length[x]], {1}] Join it all back together. ...


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

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


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

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

info[tbl_] := With[{s = SparseArray[tbl]}, ArrayPad[Append @@@ Transpose[{s["NonzeroPositions"], s["NonzeroValues"]}], {0, {1, 0}}, ToString@Unevaluated@tbl]] SetAttributes[info, HoldFirst] result=info[dataTable1] (* {{"dataTable1", 1, 1, 1}, {"dataTable1", 2, 2, 1}, {"dataTable1", 3, 3, -1}} *) As to the second part of your query, assume ...


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

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

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

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

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

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


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


6

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



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