Questions tagged [procedural-programming]

Questions about Mathematica's procedural programming paradigm.

Filter by
Sorted by
Tagged with
1
vote
2answers
115 views

For loop, use past iteration

So I'm trying to use Newton's method to find the root of a function. The code works, what isn't working is my absolute error calculation. What I need is to subtract my past iteration from the current ...
0
votes
0answers
55 views

Alternative to procedural loop with evaluation of notebooks

very new Mathematica user here. I am struggeling with programming Mathematical-like and would appreciate some guidance. I set up a main notebook which calls various sub-notebooks: ...
6
votes
3answers
440 views

Create variables in a for loop

I am very new to mathematica, but it would be much easier for me to use For loops in order to create variables. My train of thought was simple, but apparantly does not work. Let's say I defined these ...
1
vote
1answer
67 views

Code to generate many Do-loops, where the $i$-th loop has upper bound given by $f(i$)

How do I get Mathematica to run an arbitrary number of Do-loops, where the i-th loop it upper bound given by some function $f(i)$? I badly need help with this. Let's denote the desired code by ...
0
votes
1answer
58 views

RandomReal inside a loop

I want to perform the following series summation: $$\sum_{j=1}^5 \exp\left[{-\beta \, \left(\text{En} + \frac{\Delta \, X}{Num} \right)}\right] \cos{\left(\text{En} + \frac{\Delta \, X \, T}{Num} \...
6
votes
5answers
355 views

Replace elements of a list based on a function

I have a function of k (written in the form of a table/list to get values) for 16 integer values from 1 to 16: f = Table[3*k*k, {k, 1, 256}] I want to replace ...
1
vote
0answers
44 views

How do you stop a For loop from progressing until a condition has been met?

I have a matrix and the generation of a number between 0 and 1 (this number will act as a probability): ...
1
vote
1answer
122 views

Translating pseudocode for the Jacobi algorithm into Mathematica

Here is an image for the Jacobi algorithm. I have almost no knowledge of Mathematica, the textbook does not help. Does not provide examples. The class average is 64%. I just want to be able to ...
2
votes
3answers
188 views

Re-writing code chunk using While loop

I can't seem to wrap my head around the syntax for how to rewrite this chunk of code I have using a while loop, and would appreciate some help. I'm going through a list and am grouping the values into ...
1
vote
1answer
49 views

How to use syntax instead of double for loop

I would like to find the distance between a point list (i.e dalist) and list of centers (i.e framecenters). I used the following ...
1
vote
1answer
78 views

Question about the test format applied in a for loop

I have an issue with the following code. I am trying to remove the points which have a distance less than 2mm relative to each other starting with a specified point ...
1
vote
1answer
70 views

Issue with Do command

How can I substitute the arrays of a list to the frameCenter1 variable in the following Do loop? I used frameCenter1[[i]] but no answer. Assume frameCenter1 can be selected from this list: ...
0
votes
1answer
89 views

Question about for loop

How can I get the results of a "for" loop for each iteration? For example, I have the following for loop and would like to know how I can get the value of S1 for each i value in the output. Also, I am ...
8
votes
3answers
909 views

How can I replace a set of Nested For Loops with a Functional Code Alternative?

Despite having looked over various posts concerning optimizing nested For loop expressions into a functional programming equivalent I still can not figure out how to actually to do this. Such ...
1
vote
2answers
84 views

How to run 1000 examples of matrices

I have two functions of matrices, let say $f(A,\,B)$ and $g(A,\,B)$, where $A,\,B$ are positive semidefinite matrices. I want to check if $f(A, B)$ is greater than $g(A,\,B)$ for any $A,\,B > 0$. I ...
3
votes
3answers
310 views

For Loop to Sum numbers in a list

I'm asked to create a For[] loop that will sum positive numbers in a list. I am unsure of how to do this. ...
2
votes
1answer
40 views

Which condition inside a loop with unknown value in a condition

I have a Which condition inside a loop such as: ...
0
votes
1answer
60 views

Do with variables from a table

I want to perform a Do command with n variables, where n is set elsewhere in my code. How do ...
-1
votes
1answer
62 views

List with not computed numbers in Do loop

If I have a a Do loop such as: Do[ list=AppendTo[tts, otherlist] ]//Print,{j,1,10,1} which generates a list of 10 numbers for ...
1
vote
1answer
75 views

Using a condition in a do loop

If I have the following code which was written by @creidhne here Problem using TableForm and Do loops: ...
-1
votes
1answer
101 views

Problem using TableForm and Do loops

The following code is supposed generates a table based on different calculations for a given q (e.g. q[1] for this example): ...
0
votes
1answer
51 views

Problem with Do Loop and TableForm

The following code is supposed to generate 10 tables based on different calculations for different q: ...
0
votes
2answers
68 views

Current iteration number in “do” command

How can I obtain the current iteration number of the "Do" command? For example I want $N$ to be my iteration number in the following command: ...
5
votes
2answers
172 views

Code to emulate what excel does in cells

I would like to generate a code which emulate the calculations that are being perfom in columns A and B and row 7 to 12 in the attached first image. The results of those calculations are those shown ...
0
votes
1answer
90 views

Do loop and general loop question for updating a variable

My intention with the following code: ...
0
votes
0answers
39 views

RSolveValue, Table and general programming

If I have the following code: ...
1
vote
0answers
41 views

General question about RSolveValue and general programming

If I have the following code: ...
1
vote
1answer
75 views

General question about programing

If I have the following general excel columns, how could a create a program or loop in which I can generate what I have in column A and Column B as lists. "i" represents row number so that for ...
1
vote
1answer
49 views

Loop to match five point differentiation formula

I have a uniform grid (x,y) and I need help to generate a loop to compute (x-2h,y), (x-h,y),(x+h,y),(x+2h,y) for every point of x, and the same thing for every point of y , (x,y-2h),(x,y-h), (x,y+h), (...
-1
votes
2answers
62 views

If statement inside a for loop to find a function?

I am trying to find the value of two functions with two variables by using for loop. Then I want them to satisfy a condition and if that holds then I want to write only those sets of the variables for ...
0
votes
0answers
30 views

Access a variable outside a loop

If the variable "avrami1" is a column with ten elements (array of 10*1), how can I access the variable "avrami1" outside the loop and get those same ten elements? ...
1
vote
0answers
58 views

Implements the binary-searching style code in functional way

Given that I have a code snippet written in binary-searching way: ...
1
vote
1answer
114 views

Load multiple csv files and do iterative calculation for each file

I am trying to read multiple csv files and want to plot them all together, Lets say I have 3 files, file1 , file2 and file3 I want to average 3 files, and plot them all together, So far I have set ...
6
votes
3answers
527 views

How to terminate a loop when the length of a list reaches a certain length?

If I start out with a list A with $n$ elements in it, how do I append elements to it until it has a length $N$? For example if ...
1
vote
0answers
31 views

Error while exporting multiple files

I have two questions. I am trying to export results from my calculation when I iterate. I am processing multiple files using code below: ...
1
vote
1answer
30 views

Can I sample from a distribution whose parameters depend on the current iteration?

Let's say I want to sample from 100 Beta distributions, and in each successive one, the first parameter increases by 1. So given a current iteration number n, I ...
1
vote
1answer
37 views

Get all step values from a while-loop

I did a loop (first of my life) for an iterative calculation. Is it possible to get all step values as a matrix. That means the "xValue" variable should be like {215.108,219.688,...} and not only the ...
3
votes
2answers
68 views

What's wrong with While? Is it possible to use double While in a single code input? [closed]

I am supposed to get 2 in the output, but it is still -3 instead. ...
6
votes
3answers
402 views

How do I modify a symmetric matrix while maintaining symmetry?

I'm trying to figure out how to create a For loop for a symmetric matrix. I have a 5 $ \times $ 5 symmetric matrix consisting of 1's and 0's and I'm trying to ...
2
votes
1answer
441 views

Implementing “do this, then return that” in function definitions

In Python, I often use local variables in defining functions, like so: def f(x): k = initialk < 'do something to k' > return(k) I would like to do ...
2
votes
1answer
36 views

Getting general::write error while making list in for loop [closed]

So I solved following ODE $$\frac{d^2y}{dx^2}+(a^2 (5+4\tanh( x))+b^2)y=0$$ in Mathematica. Now I need to create a list for further manipulation using the above solution. I wrote the following code: <...
3
votes
1answer
35 views

More efficiently access iterator values in subkernels?

I'm looking for more efficient ways of observing values in loops in subkernels. For example, take a standard Do loop: ...
3
votes
2answers
923 views

Improve speed with a double for loop

I am trying to calculate the Euclidean distance of pairs of points $(x_i,y_i)$ with $x$ and $y$ given as separate lists. ...
4
votes
1answer
292 views

An efficient way to evaluate this expression with deeply nested Do loops

Is there an efficient way to evaluate something like this? ...
1
vote
1answer
49 views

How to deal with procedurally generated rules and patterns?

I'm trying to procedurally generate replacement rules of the following form ...
0
votes
1answer
46 views

How to use module to assign functions to a set of imported text files?

I have a lot of text files in my folder (in this example I'm using only 8). I am trying to import all of them and then do calculation on each individual file; I want to use the same notebook without ...
0
votes
1answer
49 views

Can I use StringReplace to write the number of row which I am replacing?

I have a text file which I have constructed as ...
0
votes
0answers
56 views

help with improving my code

I am fairly new to MMA-12 and have written the following code to calculate possible coordinates (for given constraints [see If[]] in the code) and get the ...
2
votes
1answer
67 views

Stop `(Parallel)Table` evaluation when wanted case is found

I have to go through a long list of evaluations, for which I suppose Table instead of For or ...
4
votes
1answer
464 views

While loop in a function

f[n_] := ( While[n < 10, n = n + 1]; n ) I am trying to learn mathematica on my own. I am trying to write a function that contains a while loop, but I ...