Tag Info

Hot answers tagged

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


8

You mean something like this? SetDirectory[NotebookDirectory[]]; m = Import["f.txt", "Table"]; getColor[m_List, i_Integer] := Module[{lim1 = 0.0001, f = m[[i, 6]], s = m[[i, 7]]}, Which[ f > lim1 && s == 0, Orange, f <= lim1 && s == 0, Red, f > lim1 && (s == 1 || s == 2), Green, f <= lim1 && (s ...


6

One can use x-Floor[x] instead of FractionalPart[x] for positive x FullSimplify[Cos[2 Pi ((i + j + k)/2 - Floor[(i + j + k)/2])], Assumptions -> (i | j | k) ∈ Integers] (-1)^(i + j + k)


6

This answer intends to answer both parts of the question. Originally it answered only the first part. Below is the original answer. Scroll down for the second part. I interchanged the third and second argument of ForAll and gave the function IsSuppFun attribute HoldAll, and it worked! So I wrote SetAttributes[IsSuppFun, HoldAll] IsSuppFun[f_[t_]] := ...


6

Yes, it is possible. However, it requires the use of the undocumented System`Private`$Localized, which was (as far as I know) first discovered by @Rojo. This symbol is most likely an internal implementation detail of the evaluator and, being located in a context that is obviously not meant for manipulation by the user, should be approached with caution. It ...


6

Something like Pick @@ Transpose@largetable~Join~{0} might do it. Unless 0 should be the Real number 0. If you have both, then try Pick @@ Transpose@largetable~Join~{0 | 0.} Edit: The above is the same as Apply[Pick, Join[Transpose[largetable], {0 | 0.}]] and has the same effect as With[{columns = Transpose[largetable]}, Pick[columns[[1]], ...


5

Different approach: list = Import["nb.txt", "Table"] lim = .0001; c1 = # > lim && #2 == 0 &; c2 = # <= lim && #2 == 0 &; c3 = # > lim && (MemberQ[{1, 2}, #2]) &; c4 = # <= lim && (MemberQ[{1, 2}, #2]) &; An array with indicators if given record fulfills given condition: cond = Outer[Apply, {c1, ...


5

A great resource about such stuff is the Tutorial: Package Design. Luckily, it was written by Todd Gayley who is also a registered member of Mathematica.SE. The important part can be found in section Error Handling If a more complex test of the input is required, you can put the Condition as the last statement in a Module: f[x_List] := ...


5

Your posted code does not work because some of the code you want to evaluate is given as an argument to Condition which holds its arguments, you could make it work by changing this f[x_] := Block[{ans, success}, ans = If[x >= 0, success = 1; Sqrt[x], success = 0]; ans /; (success == 1) ] Notice that the first line of code is process outside ...


5

A combination of Module and Condition acts very much like the proposed Block / If pseudo-code: mySet[x___] := Module[{n = DeleteDuplicates @ {x}} , mySet @@ n /; Length @ n =!= Length @ {x} ] mySet[a, b, c] (* mySet[a, b, c] *) mySet[a, a, a, b, b, b, c, c] (* mySet[a, b, c] *) We compare the lengths of the original and final lists in order to ...


4

Using the 4th argument of If should do the trick F[x_] := If[x >= 0, Sqrt[x], Defer[F[x]], Defer[F[x]]] {F[2], F[-2],F[a]} (* {Sqrt[2],F[-2],F[a]} *) or may be F[x_] := If[x >= 0, Sqrt[x], HoldForm[F[x]], HoldForm[F[x]]] so that F[a]/. a-> 2//ReleaseHold (* Sqrt[2] *) The first form (using Defer) would work also with F[a]/. ...


4

You may be able to get what you want by setting a Hold attribute on your function: SetAttributes[IsSuppFun, HoldAll] IsSuppFun[f_[t_]] := Resolve[ForAll[t, 0 <= t <= 2 && t \[Element] Reals, f[t] + D[f[t], {t, 2}] >= 0]] f[t_] := t^2 - 3 IsSuppFun[f[t]] False


4

Numerical integration in Mma is a big topic. I suggest reading at least this. The following gives the correct answer: NExpectation[(-(x*y) + z^2) Boole[x*y - z^2 <= 0], {x, z, y} \[Distributed] MultinormalDistribution[{0, 0, 0}, {{3/8, 0, 1/8}, {0, 1/8, 0}, {1/8, 0, 3/8}}], Method -> {"NIntegrate", {Exclusions -> True}}]


4

As Matariki pointed out in comments, this is a simple syntax error. The infix version of Or in Mathematica is ||, not |. Change this, like so: Xhcmaleorfemale[maleorfemale_, h_] := Which[(h < 3 || h > 7), 0, (h > 2 && h < 8), Which[maleorfemale == 0, Xhcmale, maleorfemale == 1, Xhcfemale][[h - 2]]]; And you will get ...


4

If you are not concerned by the particular color order or choice, as your conditions partition your data you could use GatherBy[]. Let your data be in variable data. You could use: gather=GatherBy[data,{#[[-2]]<=0.0001,#[[-1]]==0}&]; ListPlot[#[[All,{1,2}]]&/@gather,PlotStyle->{Orange,Red,Green,Blue}] The gather will partition your data into ...


4

Here is a solution : acceptableQ[k_, y_, z_] := Not[Or[ k == 1 && y == "low" && z == "b", k == 1 && y == "high" && z == "b", k == 1 && y == "medium" && z == "a", k == 1 && y == "high" && z == "a", k >= 2 && y == "high" && z == "b", ...


3

Just for illustrative purposes. You can modify and limit range in parameter space as required. Manipulate[ {p, q} = m; p1 = ParametricPlot[fun[x, y], {x, 0, 1}, {y, 0, 1}, Epilog -> {{Red, PointSize[0.02], Point[fun[p, q]]}, Text[fun[p, q], {3, 5}]}, PerformanceGoal -> "Quality", ImageSize -> {300, 300}]; p2 = ParametricPlot[fun[x, ...


3

Assuming you have version 9 you can do the following. data = {{-1, 0}, {0, 0}, {1, 0}, {-2, 1}, {2, 1}, {-1, 3}, {1, 3}}; dist = EmpiricalDistribution[data]; Table[Expectation[y \[Conditioned] x == i, {x, y} \[Distributed] dist], {i, -2, 2}] (*{1, 3/2, 0, 3/2, 1}*) Note: Conditional probabilities and expectations didn't work for EmpiricalDistribution ...


3

Here's my approach: The data is already quantized on a 1/100 grid so I believe we should be plotting it as such. First Import the data and extract the columns we wish to work with: data = Import["data_gRC.out", "Table"][[All, {1, 2, 6, 7}]]; Check the ranges of the coordinates: minmax = {Min@#, Max@#} & /@ Take[Transpose[data], 2] {{-0.54, 0.54}, ...


2

A general approach is to use the function Piecewise. You might define your function: pv[x_] = Piecewise[{{0, x == 11}}, -5/1.05^x] which says that if the input is 11, make the output 0, otherwise make it -5/1.05^x. You can call this with your stoptime pv/@stoptime which gives the answer you expect. Another way to do this is to define the function ...


2

Note that Power and Times have the attribute Listable, you can thus e.g. do: x^# &[Range[10]] or (the same) Power[x, Range[10]] to get: {x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, x^10} Thus, you can apply your function pvccosts on stoptime directly. In my suggestion (in the comments): pvcosts = If[# == 11, 0, (-5/1.05^#)] & /@ ...


2

I should perhaps make this post a comment and not an answer. However, I wish to fully support the comments of @AndyRoss (and have +1 his answer). cas = Cases[list, {#, y_} :> y] & /@ Range[-2, 2]; ans = {Mean[#], Mean[(# - Mean[#])^2]} & /@ cas; Style[Prepend[ MapThread[Prepend[#1, #2] &, {ans, Range[-2, 2]}], {"x", "E[Y|X=x]", ...


2

Table works much like Do -- you can have any number of statements inside. For instance: Table[r = 100; statement1; statement2; RandomReal[{-1, 1}], {i, 1, 10}] Here the r = 100 doesn't do anything, and the statements can be anything -- just separate them with a semicolon. The final term (in this case a RandomReal) occurs without semicolon and is the thing ...


2

Using the solution proposed in this answer also works: FullSimplify[Cos[2 Pi FractionalPart[1/2 (i + j + k)]], Assumptions -> {Element[i + j + k, Integers], i > 0, j > 0, k > 0}, ComplexityFunction -> LeafCount] Giving: (-1)^(i + j + k)


1

I am not all sure I grasp your question, but perhaps you're looking for MapIndexed. SeedRandom[2]; data = RandomInteger[100, {5, 3}] {{92, 57, 22}, {84, 63, 1}, {81, 96, 19}, {38, 67, 68}} MapIndexed[If[#2[[1]] > #2[[2]], 40 #1, #1] &, data, {2}] {{92, 57, 22}, {3360, 63, 1}, {3240, 3840, 19}, {1520, 2680, 2720}}


1

There were several syntax problems in your example. I constructed a simpler example to demonstrate: Manipulate[ Plot[{-1, 1, Sequence @@ If[c < 1, {x, -x}, {1/x, -1/x}]}, {x, 0, 1}, Evaluated -> True] , {c, 0, 2}] The syntax of the first argument is {func1, func2, func3 ...}. Your code would have resulted in {func1, func2, {func3, func4}}. I ...


1

I'm going to go out on a limb here and just assume that your problem is that you are nesting conditionals inside your code definitions, and that deeper levels are ignored. If that is the case, what you could do is to use a conditional check to see if your code processed without throwing exceptions, and use exceptions as your Fail[] function. mysquare[x_] ...



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