Tag Info

Hot answers tagged

7

You can do this without Table: g[k_] := k^2 + 1 Map[If[# != 6, f[#] = g[#], f[#] = 1] &, Range[10]] {2, 5, 10, 17, 26, 1, 50, 65, 82, 101} You can also use Scan in place of Map (it may be slightly faster for large lists). Note that no result will be displayed, but f will still retain the values stored in it. Scan[If[# != 6, f[#] = g[#], f[#] = ...


5

Table[{x, y, 1}, {y, 0, 1, 1}, {x, 0, 1, 1}] // Flatten gives {0,0,1,1,0,1,0,1,1,1,1,1}


5

This is a more general pattern solution that doesn't require each value after five to be larger than five: list = {9, 4, 9, 1, 2, 9, 5, 4, 4, 6}; list /. {a___, 5, b___} :> {a, 5, Sequence @@ ConstantArray[0, Length@{b}]} (* Out: {9, 4, 9, 1, 2, 9, 5, 0, 0, 0} *)


4

In addition to RunnyKine's recommendation of If here are two other methods to consider. 1: Unset or restore the value Simply Unset f[6] afterward: Do[f[k] = g[k], {k, 10}]; f[6] =. Array[f, 10] {g[1], g[2], g[3], g[4], g[5], f[6], g[7], g[8], g[9], g[10]} Note f[6] is returned as it is undefined. Or if f[6] already has a value save and restore it: ...


4

There are two easy approaches that come to mind. The first is to simply use MapThread. MapThread[foo, {Range[0, 4], Range[24, 32, 2]}] {foo[0, 24], foo[1, 26], foo[2, 28], foo[3, 30], foo[4, 32]} If you are collecting all results this is probably a perfectly good approach as memory is unlikely to be a problem. The second is to define the ...


4

This solves the problem as it has been posed: list = {1, 2, 3, 7, 9, 11, 5, 3, 5, 9}; Join[ TakeWhile[ list, # != 5 &], {5}, ConstantArray[0, Length[list] - FirstPosition[ list, 5]]] {1, 2, 3, 7, 9, 11, 5, 0, 0, 0} In case the list consitst of consecutive elements: Range @ 10 // # UnitStep[5 - #]& {1, 2, 3, 4, 5, 0, 0, 0, 0, 0} ...


3

Consider also: Flatten @ Array[{#2, #, 1} &, {2, 2}, 0] {0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1} This is faster than Table, if applicable: n = 2200; Table[{x, y, 1}, {y, 0, n}, {x, 0, n}] // Flatten // Length // Timing Array[{#2, #, 1} &, {n, n} + 1, 0] // Flatten // Length // Timing {1.435209, 14533203} {0.280802, 14533203}


3

The Plot has to be within Dynamic, as the Plot needs to be updated when z is changed. You can't just update the content of Plot without making a new Plot. {Slider[Dynamic[z], {1, 4, 1}], Dynamic@Plot[Evaluate[Table[Sin[i*t], {i, 1, z}]], {t, 0, 2 Pi}]} The syntax highlighting is due to the Head of your command inside Plot being Dynamic ...


3

Construction like this may help you. res = Reap@TimeConstrained[ Do[Sow[f[i]]; Pause[0.1], {i, 1, 30}], 1(*here number of seconds*) ] (*{$Aborted, {{f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10]}}}*) for extractoin result use res[[2,1]]


3

data= Import["http://pastebin.com/raw.php?i=7jJ4e2jW"]; titles = First@data; data = Rest@data; TableForm[data, TableHeadings -> {None, titles}] (*Displays the Table *) (*Do Something with Data*) ArrayPlot[data]


3

You can use a Table with variable iterator limit. e.g. Table[f[i, r], {i, 0, 5}, {r, {i}}] {{f[0, 0]}, {f[1, 1]}, {f[2, 2]}, {f[3, 3]}, {f[4, 4]}, {f[5, 5]}} Or Table[f[i, r], {i, 0, 5}, {r, 0, i, 2}] // TableForm f[0,0] f[1,0] f[2,0] f[2,2] f[3,0] f[3,2] f[4,0] f[4,2] f[4,4] f[5,0] f[5,2] f[5,4]


3

Of course you can use the If also inside the table: Table[If[k != 6, f[k] = g[k], f[k] = 1], {k, 10}] You can also use the If on the right hand side of the assignment: Table[f[k] = If[k != 6, g[k], 1], {k, 10}] If the whole purpose of the table is the assignment, that is you are not really interested in the list it outputs, you can replace Table with ...


2

foo = Array[{##, Dot[{##}, {##}]} &, {#, #}, 0] & foo @ 5 // MatrixForm


2

Table[{"x " <> ToString@x, "y " <> ToString@y, x^2 + y^2}, {x, 0, 4, 1}, {y, 0, 4, 1}] // MatrixForm


2

We can adapt Sjoerd's solution to the question, Table - find index of the maximum element. Other methods may be found here: List manipulation: position & max value combination. tt1 = Flatten[ Table[Thread@{x, y, z /. Solve[z^2 == x^2 y - z, z, Method -> Reduce]}, {x, 0, 5, 1}, {y, 0, 5, 1}], 2]; Then this yields {x, y, max}: tt1 ~Part~ ...


2

The simplest code I can think of is: Range@10 /. (x_ /; x > 5 :> 0) {1, 2, 3, 4, 5, 0, 0, 0, 0, 0}


1

ClearAll[f1, f2, f3, f4]; list = {1, 2, 3, 7, 9, 11, 5, 3, 5, 9}; SetAttributes[f1, {Listable}] (* redefine f1 to 0& when an input with value t is processed: *) f1[t_, x_] := Piecewise[{{f1 = 0 &; x, x == t}}, x] f1[5 , list] (* {1,2,3,7,9,11,5,0,0,0} *) f2 = MapAt[0 &, #2, {1 + Position[#2, #1, 1, 1][[1, 1]] ;;}] &; f2[5, list] (* ...


1

You wrote: It's important not to use IF checking every element whether it satisfies provided condition. I cannot agree with this, unless you mean that once the sought element is found the rest of the elements should not be checked (possibly) using If. What I mean is that even if not using If itself there is going to be some kind of by-element checking ...


1

ArrayFlatten[] takes zeroes as a representation of a zero square matrix: gamma = {{PauliMatrix[0], 0}, {0, -PauliMatrix[0]}} ArrayFlatten[gamma] Which gives {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, -1, 0}, {0, 0, 0, -1}}


1

There is a problem with the entry for terms. Retype the entry and it will work properly as below. terms = 1 + Accumulate[2. Table[1/(1 + (2 n)^2), {n, 1, 500}]]; n = {50, 100, 200, 500}; p = terms[[n]]; exact = \[Pi]/2. Coth[\[Pi]/2.]; pd = 100. (exact - p)/exact; TableForm[{{n, p, pd}}, TableHeadings -> {None, {"N", "s", "% diff"}}]


1

Too long for a comment but... For your specific setup this can be a way with a v10 function: tt1 = Table[Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5, 1}, {y, 0, 5, 1}] With[{v = z /. tt1}, With[{m = Max[v]}, {m, Most@FirstPosition[v, m]}]] But... Why don't you solve analitically the problem? The command z /. Solve[z^2 == x^2 y - z, z] ...


1

Because of this comment the following becomes too long to be just a comment so here you go: tt1 = Table[ z /. Solve[z^2 == x^2 y - z, z, Method -> Reduce], {x, 0, 5,1}, {y, 0, 5, 1}]; Max[tt1] Table[{x, y}, {x, 0, 5, 1}, {y, 0, 5, 1}][[Sequence @@ (First@Position[tt1, Max[tt1]])[[;; 2]]]] 1/2 (-1 + Sqrt[501]) {5, 5}



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