Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

Filter by
Sorted by
Tagged with
2 votes
1 answer
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 ...
user avatar
2 votes
2 answers
243 views

Storing the data at the end of the For loop

I have a For loop ...
user avatar
  • 123
0 votes
0 answers
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 ...
user avatar
  • 65
0 votes
0 answers
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 ...
user avatar
  • 1
3 votes
1 answer
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 ...
user avatar
  • 33
4 votes
1 answer
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 ...
user avatar
  • 595
2 votes
2 answers
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 ...
user avatar
  • 123
1 vote
1 answer
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): ...
user avatar
  • 1,723
2 votes
1 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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
1 vote
1 answer
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 ...
user avatar
  • 383
0 votes
1 answer
53 views

Choosing variable values from a list using Do and If Loops [closed]

...
user avatar
2 votes
2 answers
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 ...
user avatar
  • 285
1 vote
3 answers
104 views

Optimizing operations between vectors

Basically I am generating a grid in space (so a vector where v[[1]]=position 1 in x) and a random number of particles in space (so a vector where p[[1]]= position of particle 1 on space). Afterwards, ...
user avatar
  • 21
1 vote
0 answers
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 ...
user avatar
  • 21
0 votes
1 answer
119 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 ...
user avatar
  • 383
0 votes
0 answers
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 ...
user avatar
  • 443
1 vote
1 answer
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 <...
user avatar
  • 137
2 votes
2 answers
79 views

Could anyone please help with defining a vector function of tons of variables?

Could anyone please help with effectively defining a vector-valued function of tons of variables so that ...
user avatar
10 votes
2 answers
470 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). ...
user avatar
0 votes
1 answer
101 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. ...
user avatar
  • 207
0 votes
1 answer
70 views

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

Well, I am trying to run the following code: ...
user avatar
  • 1,723
3 votes
3 answers
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 ...
user avatar
  • 33
0 votes
0 answers
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 ...
user avatar
  • 445
1 vote
0 answers
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....
user avatar
2 votes
2 answers
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 ...
user avatar
  • 445
0 votes
1 answer
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 ...
user avatar
0 votes
0 answers
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: ...
user avatar
  • 47
0 votes
1 answer
64 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 ...
user avatar
  • 141
1 vote
0 answers
63 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...
user avatar
4 votes
1 answer
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 ...
user avatar
  • 271
0 votes
0 answers
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: ...
user avatar
  • 1
0 votes
1 answer
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 ...
user avatar
  • 1,047
2 votes
0 answers
102 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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
  • 53
0 votes
3 answers
402 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 ...
user avatar
  • 53
2 votes
1 answer
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 ...
user avatar
  • 1,050
1 vote
1 answer
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 ...
user avatar
0 votes
0 answers
55 views

view linebreak symbol

As you see in the above example, In[294] and In[295] looks just the same, but the outputs are different. The fact is that there is no linebreak symbol in In[294], but there is one linebreak symbol in ...
user avatar
  • 2,825
2 votes
2 answers
272 views

Simple Goto problem

...
user avatar
  • 2,825
0 votes
1 answer
30 views

How to create ParallelDo loop with n dimensions of evaluation?

For example if I have n=2 i get ParallelDo[something, {v[1], c[1]}, {v[2], c[2]}] and if I have n=3 I get ...
user avatar
0 votes
0 answers
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 ...
user avatar
-1 votes
1 answer
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 ...
user avatar
  • 149
2 votes
2 answers
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 ...
user avatar
2 votes
1 answer
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 ...
user avatar
1 vote
1 answer
49 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 ...
user avatar
0 votes
0 answers
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 ...
user avatar
  • 755
2 votes
1 answer
64 views

Matrix or loops or any ideas for help me

I have input data as below: ...
user avatar
  • 143
-3 votes
2 answers
103 views

How to change a Do loop to a For loop? [closed]

I'm a rookie in the loops. I would like to use a For loop to get a solution like using a Do loop. ...
user avatar
1 vote
1 answer
80 views

How to change a Do loop to a while loop?

I'm a rookie in the loops. I would like to use a While loop to get a solution like using a Do loop. ...
user avatar

1
2 3 4 5
7