Questions tagged [assignment]

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

Filter by
Sorted by
Tagged with
3
votes
0answers
48 views

Variable assignment/replacement issues

I have three questions about variable assignment. Module[{z}, z = y; p'[y] /. p -> Function[y, Evaluate[z]]] When I run the above code, it outputs 0. 1) If I ...
4
votes
1answer
109 views

Assignment via iteration over list of variables to be assigned values

Variations on my question have been asked before (How to re-assign values to a List of variables?, How to use Table to assign values to variables in a list?, Assigning values to a list of variable ...
4
votes
2answers
92 views

Combining SetDelayed, Set and Options [duplicate]

A function can be defined with SetDelayed while storing its value with Set, eg: ...
5
votes
2answers
70 views

Vectorized AssociateTo

With an Association we can update keys in place two ways: ...
0
votes
1answer
47 views

Create a list of indexed matrices and then change their entries

I make a list of 5 empty $3 \times 3$ matrices via Table[m[i]=IdentityMatrix[3]-IdentityMatrix[3],{i,1,5}] However when I want to change an entry via ...
3
votes
2answers
220 views

assign 2 letters in a subscript

Suppose I want to solve an equation for the matrix elements of $\bar{W}$: $$\alpha W_{ba}+\beta W_{bb}=x; \alpha W_{aa}+\beta W_{ab}=y$$ Using the syntax ...
7
votes
2answers
239 views

Apply ToString to a list of variables [duplicate]

I have data1 = {1, 1}; data2 = {2, 2}; datalist = {data1, data2}; I want to create a string list based on the variable names inside ...
11
votes
2answers
459 views

Is there a convention on which name to use for a throwaway variable? (Like underscore in Python) [duplicate]

I would prefer to write {_, foo} = LongFunctionName[arg1, arg2] instead of foo = LongFunctionName[arg1, arg2][[2]] or ...
2
votes
1answer
183 views

Define a function to be equal to the numerical solution of a PDE [duplicate]

I am curious about if it is possible to define a function to be equal to the numerical solution of a PDE (got by NDSolve)? I know that when we try to plot the ...
4
votes
2answers
79 views

Define function of a list from a function of its elements (Set)

I asked a similar question there: Define function of a list from a function of its elements (SetDelayed). This one is different because I'd like to use Set instead ...
2
votes
1answer
101 views

Switch in functions and their derivatives

Consider the following simple example: ...
2
votes
1answer
78 views

function on parts of lists works only in pure function?

For example, I'd like to take the first four upper left values of a matrix, i.e. f[x_] := f[x] = x[[;; 2, ;; 2]]; This works nicely, e.g. ...
9
votes
2answers
317 views

Duplicate the swap function

Because defining the function: swap[x_, y_] := Module[{}, t = x; x = y; y = t; Return[]] and writing: swap[x, y] {x, y} I ...
4
votes
2answers
98 views

Assigning values to association in DownValues

Compare the following code: Clear[t]; t = <||>; t[2] = 1/2; t[2] with ...
3
votes
2answers
86 views

Find the input definition

Consider the following definition a = D[x,x] Now, a=1. Is there a way in which I can recover the information about the input ...
1
vote
1answer
56 views

Variable assignment to a function

I am trying to do a seemingly simple thing: f[x_, y_] := x + 3 y /. {x -> -x y} the output for $x=1$ and $y=2$ is 7, so it doesn't make a substitution. How ...
8
votes
2answers
442 views

Problem with function inside brackets. Bug? [duplicate]

This code adds random digits to lists, and it works fine: ...
5
votes
1answer
276 views

clear variables on parallel kernel

Short question: what is the proper way to clear a variable on a given kernel ? AND how can I ensure that the memory will indeed be freed subsequently ? Full question. Let me elaborate this question ...
0
votes
1answer
49 views

Using Set without unpacking an array

I'm trying to optimise my code by making us of packed array. However, it appears that using Set in combination with Part ...
1
vote
2answers
215 views

How to return the unevaluated variable names from a list of variables which have been declared

I feel like this should have a simple answer but I haven't found anything that works. The toy example of what I want is as follows: ...
3
votes
1answer
66 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
214 views

What's wrong with the Sum function?

Here are the codes: ...
1
vote
2answers
196 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
104 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
44 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
159 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
115 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
107 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
227 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
64 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
130 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
80 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
82 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
152 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
141 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}}. ...
1
vote
2answers
628 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
63 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
125 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
128 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
134 views

How to assign options to a variable?

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

Need explanation on strange behavior of Do together with AppendTo

We didn't expect a replacement of B. ...
0
votes
1answer
74 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
79 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
271 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
47 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
119 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
100 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...
14
votes
3answers
294 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 ...