Questions tagged [assignment]

For questions relating to assignments to symbols, patterns, or expressions.

Filter by
Sorted by
Tagged with
3
votes
1answer
77 views

Using HoldFirst at the same time as Apply

I have some code which pops the first number from a list, displays that number, and updates the list: ...
7
votes
2answers
246 views

What's wrong with the Sum function?

Here are the codes: ...
1
vote
2answers
223 views

Evaluating ReplaceAll inside SetDelayed, first

Consider the following (simplified for the purpose of example) definitions: f[x_] := y /. y -> x g[x_] := x Where for example, the definition of ...
6
votes
1answer
107 views

Different Plot's output defining a function

I want a plot with this function -((H^3 T)/(200 Pi)) + (H^2 T^2)/96 + H^4 l + H^2 m with ...
1
vote
1answer
47 views

Solving for a function after replacing and substituting in an expression [duplicate]

I am trying to isolate $X(s)$ in the following expression: ...
1
vote
0answers
200 views

How can I assign a value to a multivariable function in Mathematica corresponding to only one of the variables? [closed]

If I consider a function for example: g[x_]:=x^2 and fix g[x0]=10.0 at a certain point x0, ...
2
votes
1answer
125 views

How to set a symbol indirectly?

I have a symbol, say x, whose calculated value is a 2nd symbol. How can I set the 2nd symbol to a given value? For example, say the value of x is one of the symbols a, b, or c. If it is a, I want to ...
1
vote
1answer
113 views

Use Table to set Imported Variable? [closed]

I'd like to assign a SemanticImport dataset to a variable on import. Right now, I have this, and it doesn't work: ...
2
votes
3answers
255 views

How can I assign many values each to a different variable? [duplicate]

How can I assign many values each to a different variable? For example, I want to set $a = f(1),\,b = f(2),\,c = f(3),\,\dots,\,z = f(26)$ (generally more variables than these). How can I do this ...
1
vote
1answer
66 views

Assigining values after the Apply operator

I am writing a script where I need many variables and for ease of usage I denote them as f[k,l,m,n]. I create a list of 4-tuples with the integers I need and I call ...
3
votes
1answer
147 views

Programmatically assign output Hold[…] produced by errors to a variable

expr = Return[1, 2] Return::nofunc: Function 2 not found enclosing Return[1,2]. >> (*Hold[Return[1, 2]]*) However, ...
3
votes
1answer
87 views

Why does syntax highlighting in `Set` and `Rule` not color pattern names on the RHS?

The mathematica front-end colors x on the right hand side of x_ :> x and f[x_] := x but ...
1
vote
1answer
86 views

Part function: can we use pattern for part functions? [duplicate]

Can we use some patterns for the arguments of Part functions? ...
6
votes
2answers
157 views

Association matching still not working perfectly?

Bug introduced in 10.4 and persisting through 11.1.1 Up until 10.4 you could not match on internal Association structure at all: https://mathematica.stackexchange....
2
votes
2answers
146 views

How to create assignments to OverVector programmatically

I wish to write a function that accepts two lists. The 1st argument is a list of lists, holding the components of a several vectors. Ex.: comp = {{a, b}, {c, d}}. ...
2
votes
2answers
854 views

Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the ...
0
votes
1answer
68 views

How to use Set correctly

My codes are shown in the picture attached. I'm writing codes for numerical calculations frequently, and I think it looks neat and clear to organize input parameters in the form of table. For example,...
5
votes
1answer
148 views

Using delayed evaluation on module variables called through an association

I came up with a construct that works well enough for my current use case but has some flaws (at least I suspect it does). Say we have a function that calculates some stuff that takes a while to ...
8
votes
2answers
137 views

How do I SetDelayed $Assumptions

For some reason, $Assumptions := (Print["y"]; x > 0) evaluates the RHS immediately. I have code in which $Assumptions ...
1
vote
1answer
194 views

How to assign options to a variable?

I want to redefine a function similar to Plot like this: ...
2
votes
2answers
115 views

Need explanation on strange behavior of Do together with AppendTo

We didn't expect a replacement of B. ...
0
votes
1answer
80 views

Iterate a large matrix calculation [closed]

I want to repeat the calculation of a pretty big formula about 100 times. I tried using Do loop but it takes sooo much time that mathematica stops working. I will take a simple example to illustrate ...
2
votes
1answer
81 views

How to determine where a variable got assigned a value?

I have a Mathematica notebook that includes some dynamic structures (with Manipulate). Sometimes, I discover that x has been ...
3
votes
1answer
313 views

Is there any way to implement a “sequential” With[] in Mathematica? [duplicate]

I want the equivalent of Scheme's let*, or basically, a sequential With that works like this: ...
10
votes
6answers
2k views

How do I randomly divide individuals into groups?

I would like to assign 'x' individuals to 'y' groups, randomly. For example, I would like to divide 50 individuals into 100 groups randomly. Of course, with more groups than individuals many of the ...
47
votes
1answer
5k views

++ is dangerous for C programmers

I noticed this fact, that may be misleading for programmers used to C language. In Mathematica, if you have a function f[] and an array ...
0
votes
1answer
49 views

Where is the erroneous assignment that is causing my 'List of equations expected instead of tru' error?

I'm getting one those annoying 'NDSolve::deqn: List of equations is expected instead of True' errors. I have done everything I can think of to Remove and Clear all variables, including restarting ...
4
votes
2answers
139 views

Append fixed value to random selection of list

(This question might be an application of this other question, but there might be another solution specific to this application that I'm not aware of.) I am currently in a situation in which (in a <...
2
votes
1answer
103 views

Writing my own error and warning messages

Question: How is message handling implemented in Mathematica when applying Set to unprotected global variables such as $TimeZone...
16
votes
3answers
336 views

Why is the first argument of the SetDelayed evaluated?

In[1]: f=g; In[2]: SetDelayed[f[x_],x^2]; In[3]: ?f Global`f f=g In[4]: ?g Global`g g[x_]:=x^2 However, SetDelayed has the ...
9
votes
4answers
529 views

Looping through 3 million elements of a list to 'Set' values is slow

I am looping through the 3 million elements of a time-series (v1, o1) and corresponding values (v2, o2). v1 contains a uniformly increasing timestamp (e.g. 1 to 3,000,000 in increments of 1) o1 ...
3
votes
1answer
127 views

Table generation with SetDelayed function [closed]

I would like to understand why the following two outputs differ: Let us define the following data set: ...
6
votes
2answers
263 views

Force evaluation of the right-hand side of a local variable definition

One can do expression = a + 1; f[a_] := Evaluate[expression] but how would one do something like this ...
0
votes
1answer
312 views

Correct definition of a function to be passed to Nintegrate [duplicate]

If I type the following code ...
5
votes
3answers
603 views

Turning an expression into a function inside another function

Say I have an expression g=Sin[q]; It is easy to turn it into a function, and to perform manipulations on this function (this and variants of this were discussed ...
1
vote
3answers
174 views

Setting ::usage for variables returned from a function

What is the best way to achieve {v,u} = f[]; (* Returns value and usage description *) v::usage = u; from inside f, i.e., <...
9
votes
2answers
317 views

Passing an unevaluated part of an association to a function

I'm making a UI widget that controls sound volume on a speaker using a slider, e.g.: ...
0
votes
1answer
380 views

Variable dependency…easy one

I'm brand new to Mathematica...should be an easy one. Instead of writing u[x] everywhere in the code, how do I assign the dependency early on like this: u=u[x] ...
4
votes
2answers
135 views

Initializing several values of a function

How do I initialize several values of a function at once? Consider the function f[0] := 1; f[1] := 2; f[2] := 3; f[n_] := f[n] = f[n-1] + f[n-2] + f[n-3]; Is ...
7
votes
2answers
663 views

Assignments in ParallelDo when using indexed variables

For convenience I use an indexed variables syntax to hold results of long computations for different integer parameters: ...
9
votes
1answer
950 views

How is LHS = RHS; … ; LHS (nontrivially) different from … ; RHS

I'm trying to understand how Mathematica thinks about the results of Set. Suppose we perform LHS = RHS. I'm focusing on when ...
3
votes
0answers
41 views

Out[x] not the same as assigned expression [duplicate]

There seems to be a difference in the way Mathematica assigns a value to a variable and the way it assigns a value to the next Out object. I don't understand why this is happening. For instance, take ...
3
votes
1answer
870 views

Is there a way to simplify a symbolic expression at assignment, once-for-all?

I have a series of symbolic function calls that could be greatly simplified using some assumptions. I think I would like for these assumptions to be used in the definition of the function so that they ...
1
vote
1answer
151 views

Is it possible to assign a value where the target is itself a variable

If I have a variable x := "a". Is there a way I can use it to assign a value to a, by just referring to ...
7
votes
2answers
205 views

Problem with Simplify, Sqrt, and Set: What's going on?

I am having some trouble getting Mathematica to simplify an expression of the form Sqrt[x]*Sqrt[1/x], where x>0. The problem ...
4
votes
3answers
425 views

Using Block with a list of variables

I have a list of variables var={v1,v2}; which I want to protect/localize during a particular operation. I have decided that I want to use ...
1
vote
0answers
29 views

Assign to variables inside Array, rather than changing array entries [duplicate]

I am generating an Array with symbolic coefficients: coefficients = Array[c, 3] > {c[1], c[2], c[3]} I would like to assign a value to each of these generated ...
0
votes
2answers
644 views

How to assign the values for the Table list?

Now, I want to assign some values for a Table list and I find that If {mat[1], mat[2], mat[3], mat[4], mat[5]}={4,23,3,12,1}; it is no problem. But, if we use ...
13
votes
1answer
279 views

Why can AppendTo modify a referenced list in-place but Part cannot?

Part, AppendTo, PrependTo, AddTo, etc. allow in-place modification of a list, but only Part requires that the list be referenced through a simple symbol, e.g. the following all does what you'ld expect:...
1
vote
1answer
147 views

Simultaneous use of Set AND SetDelayed [duplicate]

Tha Mathematica documentation gives a good example of the simultaneous use of Set and SetDelayed in dynamic programming for the Fibonacci sequence under "neat examples" for SetDelayed ...