New answers tagged

0

This is a different approach, that seems to be very fast n = 5; u = Sin[Range[n]] // N; (Join[1/(u[[#]] - u[[1;;# - 1]]), {a}, 1/(u[[#]] - u[[# + 1;;]])])& /@ Range[n]


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


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


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


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.


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


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


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


0

Interpreting the question as kglr has done, and using listA and listB as given in kglr's answer: Cases[listA, {#, x_} :> Sequence[#, x]] & /@ listB /. {} -> Null {Null, {10, 0.239651}, {1, 0.00869692}, {9, 0.181763}, {9, 0.181763}, Null, Null, {6, 0.074256}, {3, 0.0413228}, Null} Or Cases[listA, {#, x_} :> Sequence[#, x]] & /@ ...


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.


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

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


3

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


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


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.


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


0

I am not sure what the exact aim is. In the following it is assumed: 2 random samples from a range of integers (distinct elements) of equal length the aim is to determine size of intersection: f[n_, s_] := Module[{a = RandomSample[Range[n], s], b = RandomSample[Range[n], s], j, u, c, r}, c = Intersection[a, b]; r = # -> Style[#, Bold, Red] & ...


0

Now that the question has been restated, you can use a shortened version of the answer suggested by mgamer, B = {2, 10, 2, 5, 7, 15, 1000, 7, 25, 600}; A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Count[B, #] & /@ A // Total (* 6 *) Or you could use MemberQ and Boole, Boole@MemberQ[A, #] & /@ B // Total (* 6 *) Or Select, MemberQ, and Length Select[B, ...


3

For anything but the simplest of graphics objects, always avoid Animate and use ListAnimate instead. The difference is that ListAnimate works on a pre-defined list of images to create an animation. All the rendering is done beforehand. With Animate, it attempts to do the rendering on the fly, when you are moving the slider. So this will make the ...



Top 50 recent answers are included