New answers tagged

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


6

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


3

When dealing with a list of rules ({a->b, c->d, ...}) you might also be interested in converting it first into an Association, which allow efficient treatment of its elements and with some more simple syntax. For example in your case: taking @March example: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 ...


4

f = # -> #2^2 & @@@ # &; poly = Plus @@ Array[# a[#] x^# &, 5]; c = CoefficientRules[poly, x] {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} f@c {{5} -> 25 a[5]^2, {4} -> 16 a[4]^2, {3} -> 9 a[3]^2, {2} -> 4 a[2]^2, {1} -> a[1]^2}


7

Here are a couple of options. Given: poly = Array[# a[#] x^# &, 5] (* x a[1] + 2 x^2 a[2] + 3 x^3 a[3] + 4 x^4 a[4] + 5 x^5 a[5] *) We can do c = CoefficientRules[Expand[poly], x] c /. HoldPattern[a_ -> b_] :> (a -> b^2) (* {{5} -> 5 a[5], {4} -> 4 a[4], {3} -> 3 a[3], {2} -> 2 a[2], {1} -> a[1]} *) (* {{5} -> 25 a[5]^2, ...


1

Many thanks to @J.M. and @Artes I found out that the iterator 0.1 in the Table function was making this artifact, which is also version dependent. @J. M.'s solution worked perfectly: Table[N[alpha[y1, d1]], {d1, 1/10, 5, 1/10}, {y1, 0, d1, 1/10}]


3

Do not use x as both a dependent variable in NDSolve and an index in Table. Instead, try, Table[Solu[-a, -1, 1 + a, .3, 50], {a, 0, 2}] (* {{7.19845*10^-22, -0.102113, 0.102113, 3.31597, 3.31597, 3.26406}, {-0.10725, -0.10725, 0.214501, 3.33333, 3.16227, 3.16227}, {-67.8836, -67.7287, 135.612, 3.29475, 0.133514, 0.162306}} *)


1

The three solutions provided in comments are all perfectly viable, and their performance is comparable as well: nmax = 3000; Select[Table[{n, Abs[2^n - 51]}, {n, 0, 3000}], PrimeQ[#[[2]]] &][[All, 1]]; // RepeatedTiming Select[Range[3000], PrimeQ[2^# - 51] &]; // RepeatedTiming Pick[#, PrimeQ[2^# - 51]] &@Range[3000]; // RepeatedTiming ...


0

Here's a start. Since you declined to specify f[ ], I chose one. f[x_, y_] := x^2 y^2; y = Table[{c1 + c1 + inc, f[c1, inc]}, {c1, 0, 3, 0.1}, {inc, {0, 0.2, 0.4, 0.6}}]; ListLinePlot[Transpose[y]]


1

Look up the difference between Append and AppendTo Append doesn't reassign the value of the List, while AppendTo does.



Top 50 recent answers are included