# Questions tagged [procedural-programming]

313 questions
Filter by
Sorted by
Tagged with
78 views

### Loop over multiple variables with readable code

I've seen similar posts to this, but not with as many variables. I need to loop over 8 variables, with only two values each(1 and 2), so I can loop over the 16 possible input and output states, and ...
243 views

### Storing the data at the end of the For loop

I have a For loop ...
50 views

### Algorithm consisting of modules returning multiple errors

I'm trying to write a genetic algorithm that will take a list of lists of reals in interval [0,1], my "starting population", "score" them according to a fitness function, append ...
76 views

### While loop to solve a diffusion problem

Crossposted at Wolfram Community: https://community.wolfram.com/groups/-/m/t/2506329 I am trying to use a While loop to solve for a diffusion problem using the power iteration method. The code is ...
45 views

### Trying to add up values into a matrix

first post here. I'm currently completely stuck and frustrated with mathematica. In retrospective, it might had been a mistake to use it for this, I'm actually considering to somehow export the ...
91 views

### Efficient iteration of matrix multiplication into a table

Suppose I have a list of matrices $(A,B,C)$ and a vector $v$. I want to construct a table whose elements are sequential applications of these matrices to $v$, i.e., the first entry will be $Cv$, the ...
372 views

### How to put a FOR loop in a WHILE loop

I have a strange matrix like this one in the picture Now, my goal is to change all the 1s with random complex numbers of modulus 1, but if the element $a_{12}=z$, then $a_{21}= \bar{z}$, so the ...
1 vote
43 views

### Check certain expression using a while loop to run through all posibilities in a range

Well, I have written the following code (using the fast square root test found in this answer): ...
45 views

### How to convert a While into a NestWhileList

How do you rewrite this While statement with NestWhileList? I know you can easily get this done with ...
170 views

### Calculating successive derivatives of a monomial list using a Do loop

I've got the following monomial list: monlist={c x^3, x^5, 4}; I need to compute the successive derivatives of that list of monomials such that when the derivative ...
1 vote
116 views

### How to calculate Jensen-Shannon divergence?

How to write the function (or any other methods) to calculate Jensen-Shannon divergence (JSD) for two (p and q) discrete probability distributions? I need to calculate JSD for too many different (over ...
53 views

...
183 views

### How to put Break command within a Do loop over 2 variables

Suppose I have the code Do[If[f[i,j]==1,{output[i]=j,Break},Continue],{i,1,5},{j,1,1000}] Where $f$ is just a function. I want the code to find the first $j$ that ...
1 vote
104 views

### Optimizing operations between vectors

Basically I am generating a grid in space (so a vector where v[]=position 1 in x) and a random number of particles in space (so a vector where p[]= position of particle 1 on space). Afterwards, ...
1 vote
64 views

### How can I make ParallelDo faster than Do?

I am trying to optimize my code as much as possible. So first I used a compile and now I am trying to parallelize the code in order to make it faster because in the end I will need to make a loop that ...
121 views

### How to use for loop in Mathematica?

in most of my job I have to repeat something for different part of data, for example making plot, usually the only thing need to change is index. Below is the example of the code which I have to ...
38 views

### Concatenate string with the value of each element of a vector/list

I have a vector/list t = {4, 5, 6}. I want to output each element (its value) of t in a string of characters. For example, the ...
1 vote
49 views

### Why each iteration takes longer than previous one when using VariationalD in this loop?

I have a function $f$, and I would like to modify it iteratively to approach another one called $a$ (not because I think this is the best way to do anything, but for experimentation). For this, I use <...
79 views

Could anyone please help with effectively defining a vector-valued function of tons of variables so that ...
471 views

### What is the most efficient way to define a Loop until function?

Mathematica has the While function which is very convenient when we need to process some code until the attainment of a certain state (identified with a condition). ...
102 views

### An example code for doing a loop [closed]

I would like a code that works for doing loops as in the toy example below. This would help me to understand how to work with loops in more general contexts as I am still a beginner in Mathematica. ...
70 views

### Parallelization in While[] loop when testing if a condition is met

Well, I am trying to run the following code: ...
136 views

### Do loop so that I do not get double terms?

I have a Do loop where I generate quantum state equations <111||111>,<111||112>...<232||232>. This is 144 equations in total but I want both bra and kets to be seen as the same so ...
80 views

### The use of NestList

After read Michael answer, code use NestList to compute and feed in numerical integration. I want to learn this idea into my code. In the ...
1 vote
50 views

### Loops Not Working- Kernel Crashes and Values Incorrectly Updated

I haven't used Mathematica in a while so any help is very greatly appreciated. I am trying to run a program with nested for loops that runs two function evaluations in the inner most part of the loops....
94 views

### How to realize NDSolve by NSolve with the implicit constraint equations?

Daniel post the amazing code to realize numerical integrators, I think it is useful to the kind of one-step explicit equation like x[k+1]-x[k]== constant_number. In ...
48 views

### How to compose an algorithm to create new array (based on array data), if I need a reference to an array being created, no for loop, in mathematica?

I have an array with either 1 or 0. From this, I am trying to create a new array with intervals. I have some base interval, one step, we can say 1 second here. If there is 0, I skip, if there is 1 I ...
54 views

### How to use Mathematica to return value from for-loop [duplicate]

I tried to use Mathematica to implement random generator. The pseudo code I use is as following: ...
65 views

### Evaluation self notebook with loop

I have a large notebook with multiple sections. I would like to run the entire notebook with one variable change kquads. At the end of the notebook, I added one loop ...
1 vote
64 views

### "With" use in a (implicit) nested loop

Symbolic code: Do[g[longcat[i,k],longcat[i,k]],{i,1,f[k]},{k,1,m}] Longcat is loooong and occurs several times in my expression g...
105 views

### Binary permutation list code in Mathematica

Given some natural number $N$, I am interested in the set of all binary permutations of length $N$ (with the intention of storing in lists depending on how many $1$'s appear in each permutation). My ...
56 views

### matrix problem: evaluation of column values which depend on a variable

I do not use Mathematica for a long time and I have a question about a matrix problem. I add here the code: ...
73 views

### How to clear all things inside a loop?

It happens to me a lot when I write a loop, I have to Clear all functions, variables, definitions and etc to be sure my calculations is done correctly. namely as a ...
103 views

### Building a Matrix as a piecewise-function of the indices without for loops [closed]

I'm very new to Mathematica and this site. I've read that building matrices with For loops is slow, and I have experienced this myself on the current project. I'm trying to wrap my head around ...
103 views

### How to implement If criteria for the NestList-Function

I have a function which bisects a list in two parts recursively. Unfortunately I have to select a number of iterations for the NesList-function which is just an ...
403 views

### Python recursive bisection algorithm to Mathematica

I've been sitting on this problem for a while and i am kind of stuck. I am pretty new to Mathematica and have been trying to implement a recusive Algorithm, which splits/bisects a list of items in ...
177 views

### Cannot assign variable value inside Do Loop

This is more of a conceptual problem. Suppose I have three lists. x1 = {1, 2, 9, 10}; x2 = {5, 6, 7}; x3 = {9, 10, 13}; Now, I wish to keep only the first two ...
1 vote
44 views

### Increment FindMinimum range

I have a function SQ[b,zQ,zh] where I want to find at which zQ it is a minimum. The parameters are ...
55 views

### view linebreak symbol

As you see in the above example, In and In looks just the same, but the outputs are different. The fact is that there is no linebreak symbol in In, but there is one linebreak symbol in ...
272 views

...
30 views

### How to create ParallelDo loop with n dimensions of evaluation?

For example if I have n=2 i get ParallelDo[something, {v, c}, {v, c}] and if I have n=3 I get ...
53 views

### Check an input matrix M contains any nonzero diagonal matrix element or not

Is there a way to easily check an input matrix M contains any nonzero diagonal matrix element or not? For example, if the M contains any nonzero diagonal matrix element, Output: True. if the M ...
79 views

### Costly function inside nested Modules [closed]

The function initDataTable simply reset all data to Black first, then set the bottom right quarter to ...
69 views

### How can I store vectors in a matrix

I am triying to generate realizations of a Gaussian random process using the KL expansion. For that, I need to multiply an eigenvector for an eigenvalue and a random variable. I have tried ...
137 views

### Repeating and Sequencing Procedures with two Do-loops

I have a Do loop where I'm looking for the value of certain parameters that respect the conditions inside an If, and with those values, a run another Do to make another calculation of an expression ...
1 vote
51 views

### How to get a loop for a differential equation system?

I would like to know how can I obtained a loop of this system ...
58 views

### Replacing the print display in a for loop

I am running a For loop as follows: table = {}; For[n = 0, n < 10, n++, {table = Append[table, n], Print[table]}] I was wondering if there was a convenient way ...
64 views

### Matrix or loops or any ideas for help me

I have input data as below: ...