Hot answers tagged

7

You can safely ignore the warning and red highlighting. It simply tells you that the variable will be injected into Block by the top-level rule (your function), rather than being the actual symbol originally present in Block's declaration list. Which is exactly what you want here. In most cases, things like that happen due to a programmer's mistake, which ...


7

If you need to get involved with a function of index j, you might find MapIndexed helpful. For example, if you want to drop j th element in the jth sublist, you can do MapIndexed[Drop[#1, #2] &, tab] Here #2 is your {j}, and you can feed it to your function. Be aware of the difference of {j} and j in Drop.


6

Verily, this is a headache, since Table wants its iterators as Sequence rather than nested list of lists. Here is a method I used quite recently to get sufficiently fast code for this. c1c[n_] := With[{itvals = RandomReal[{0, 1}, {n, 5}]}, With[{iters = Apply[Sequence, Table[{x[j], itvals[[j]]}, {j, n}]]}, c1[n] = cCompile[{}, tTable[1, ...


5

Normal[SparseArray[{{i_, j_} /; j != i :> 1/(Sin[i] - Sin[j])}, {5, 5}]] Non-zero diagonal, e.g. Pi: Normal[SparseArray[{{i_,i_} -> Pi, {i_, j_} /; j != i :> 1/(Sin[i] - Sin[j])}, {5, 5}]]


4

If we are to drop the first 2 elements of tab[[1]], the last 2 of tab[[2]], the first 6 of tab[[3]] and the last 2 of tab[[4]] make use of MapThread, e.g. MapThread[Drop, {tab, {2, -2, 6, -2}}] {{0.287282, 0.29287, 0.349432}, {0.335918, 0.320225, 0.306177, 0.294094, 0.28371, 0.274858, 0.267383, 0.260873, 0.255252, 0.247822, 0.245063}, {0.548742, ...


4

The first thing you should do is to forget about using For-loops to manipulate lists. If you feel more comfortable with For-loop type thinking then you might use Table[Drop[tab[[i]], -2], {i, Length[data]}] But if you are willing to learn a bit functional coding, I'd recommend Drop[#, -2] & /@ tab


4

Thanks to J.M. I found a more elegant way to obtain the statinoary values that returned far simpler data, which was easy to plot. This is how my code looks now: ecf = s (p[t] - f[t]); ecp = -p[t] + d[t] f[t]; ecd = b (r - d[t] - f[t] p[t]); par = {s -> 3., b -> 1, r -> 100}; t0 = 800; tf = 850; estables[ra_] := Module[{par, solnum, points}, ...


4

I would prefer to use Array over SparseArray or Table here, because this matrix isn't really sparse and you are indexing over consecutive integers. For example, if you're okay with non-Ifconditional forms, I would use Array together with Piecewise as follows: f[i_, j_] := Piecewise[{{0, i == j}, {1/(Sin[i] - Sin[j]), True}}]; m1 = Array[f, {5, 5}] When ...


3

Update: Timings ClearAll[f1, f2, f3, f4] f1 = Block[{Power}, Power[0. | 0, -1] = #; Table[1/(Sin[i] - Sin[j]), {i, 1, #2}, {j, 1, #2}]] &; f2 = With[{v = #, n = #2}, Array[If[#1 == #2, v, 1/(Sin[#1] - Sin[#2])] &, {n, n}]] &; (* Rashid's answer*) f3 = SparseArray[{{i_, j_} /; j != i :> 1/(Sin[i] - Sin[j])}, {#2, #2}, #] ...


3

This should be much more efficient for anything beyond small lists: (Tr[Tally[#1~Join~#2][[;; Length@#1, 2]]] - Length[#1]) &[listA, listB]


3

This is a very quick-and-dirty, but gives 4X speed-up (7X with tweak for symmetry) on my crappy netbook for the n=8 case, don't have time or patience to test bigger cases to see scaling differences. Perhaps a description of what you're trying to calculate? It appears to be some combinatorial problem, there may well be a much more efficient scheme to do ...


2

There are several ways, including For and Do loops. But you can pack all your calculation work in a Module (a function) and then fill it with the table command: produceOutput[x1_, x2_, x3_] := Module[{center = {0, 0, 1}, line, surface, intersection, interPoint, d, data}, center = {0., 0., 1.}; line = Line[{{x1, x2, x3}, center}]; surface = ...


2

For a shorter example, lets take A and B to have 20 elements instead of 100, and have the integers in B run from 0 to 40, nElements = 20; maxB = 40; listA = Table[{n, RandomReal[]}, {n, nElements}] listB = RandomInteger[maxB, nElements] (* {{1, 0.350025}, {2, 0.651077}, {3, 0.444575}, {4, 0.261574}, {5, 0.40258}, {6, 0.670888}, {7, 0.388662}, {8, ...


1

I Have finally found what was causing the problem. it was with notation. I define my constant a_o but in the equation I have used a_0. Once I changed that it was working.


1

f = With[{l1 = #, l2 = #2}, If[MemberQ[l1[[All, 1]], #], l1[[#]]] & /@ l2] &; SeedRandom[3] n = 10; probs = Differences@Join[{0}, Sort@RandomVariate[UniformDistribution[], n - 1], {1}]; listA = Transpose[{Range[n], probs}] {{1, 0.00869692}, {2, 0.130583}, {3, 0.0413228}, {4, 0.166427}, {5, 0.0572686}, {6, 0.074256}, {7, 0.0501474}, {8, ...


1

For a solution that follows your algorithm to the letter, you would need something like this: Map[ If[ MemberQ[ A[[;; , 1]] , #], AppendTo[c, A[[#]]]] &, B] I used c instead of C because C is a protected symbol.


1

The previous answers seem to have been addressing a rather different (or much more generalized) sort of problem, because as I see this now, we have the following problem statement: len = 10; (* let's have 10 to be specific, but this is an arbitrary positive integer *) max = 10^3; (* maximum value of numbers in listB *) listA = Range[len]; listB = ...



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