Linked Questions

0
votes
1answer
579 views

Assigning value to an array in nested for loop [duplicate]

I want to write a code like X={}; index=1; max=3; For[i=1,i<=2,i++, For[j=1,j<=2,j++, X[[index]]=i*j; index++ ] ] But I am not getting ...
0
votes
1answer
46 views

An elementary question about For loop [duplicate]

Assume we have this For loop: For[i=1,i<10,i++, i^2]. How can we put the squared values ...
472
votes
37answers
107k views

What are the most common pitfalls awaiting new users?

As you may already know, Mathematica is a wonderful piece of software. However, it has a few characteristics that tend to confuse new (and sometimes not-so-new) users. That can be clearly seen from ...
187
votes
3answers
42k views

Alternatives to procedural loops and iterating over lists in Mathematica

While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
40
votes
8answers
3k views

Can this be written well, without loops?

Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so. ...
59
votes
5answers
6k views

Are there any cases when For[] loops are reasonable?

To my mind the only reason for the existence of For[] loops in Mathematica is to allow new users with some experience in procedural programming languages to write ...
7
votes
8answers
540 views

Finding C[i]'s expression in DSolve solution

An example, the command DSolve[y''[x] - 4 y[x] == 1, y[x], x] gives solution ...
9
votes
2answers
677 views

Optimizing Mathematica Code

I have implemented the whole Baum-Welch Algorithm for training Hidden Markov Models as described in Rabiner's paper Here is my code that calculates the Forward Probabilities` ...
5
votes
3answers
1k views

How to speed up a process

I am wondering how to speed up the following code: Currently it takes more than two sec to be processed. ...
6
votes
1answer
529 views

An efficient way to translate a triple for-loop from Matlab to Mathematica

This is my first post on StackExchange, so if I violate any etiquettes, I apologize in advance. I am a senior engineering student who has been using Mathematica for a year now. For one of my classes, ...
5
votes
7answers
340 views

Computing only Prime Powers with NestList

Is there a simple way to compute only prime powers of a function f with NestList? That is, I want to compute: {f[x_0], f^2[x_0]=f[f[x_0]], f^3[x_0], f^5[x_0]...} ...
3
votes
2answers
1k views

For Loop and Sum

I am requested to do a sum of the first fifty positive even numbers by using For loop. The For loop that I created is ...
7
votes
2answers
505 views

CUDA image processing

I am having an issue with image processing involving CUDALink. ...
13
votes
1answer
646 views

Pascal records and Mathematica programming

Background: "experienced" Pascal-programmer (DOS-era, never got into OOP) proud owner of Mathematica ver 11.2 have used Mathematica for years to do my share of number crunching and plotting to ...
5
votes
2answers
567 views

Runge-Kutta implemented on Mathematica

I am trying to solve differential equations numerically, so I am trying to write a 4th -order Runge-Kutta program for Mathematica (I know NDSolve does this, but I ...
9
votes
2answers
277 views

Is there a way to create ZIP OutputStream ?

I want to write data to a file using compression. Is it possible to do that just in time (like in java ZipOutputStream). E.g. what I want: ...
12
votes
1answer
681 views

Why does NDSolve fail to solve the PDEs and spit out mconly warning?

I try to solve two coupled PDEs with NDSolve using the following code: Set two operators: ...
6
votes
3answers
360 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 ...
8
votes
2answers
205 views

Converting a backward/forward sweep code for optimal control to _Mathematica_

I'm trying to convert to Mathematica a simple backward/forward sweep (with RK4 integrator) code to solve an optimal control problem in epidemiology, but I'm getting some error messages and I cannot ...
3
votes
1answer
2k views

Selecting elements from a list of lists [closed]

Being a novice, I'm having trouble with the following: I have a list of lists and want only those elements where the second element of the second level is positive. So, for example, from ...
3
votes
7answers
242 views

How can i change some numbers in a list?

I do not want to change all of them just some of them in particular rule. For example the list is given a={1,2,3,4,5,6,7,8,9....,100} If the number is bigger than ...
2
votes
2answers
700 views

For/ Do loop on a lists of two variables

I am trying to get an output of the following kind Out = Do[Evaluate[f[x,y]],{x,table1},{y,table2}] Here Out is a N-times-1 dimensional array/list and N is the ...
5
votes
1answer
428 views

Random walk in one dimension — meeting of two particles

I'm new to Mathematica and still teaching all by myself, hope you bear with me. I have the code: ...
3
votes
3answers
201 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. ...
4
votes
3answers
455 views

Dot products and For-loops

I'm taking a class where we're working with matrix applications inside of Mathematica. In this case, we're analyzing senatorial votes that have been numericized so that a value of 1 corresponds to "...
3
votes
2answers
257 views

Print characters from list with a For-loop

Suppose I have the list primary = {"b","a","e"} I want to get the following output with a For-loop (or While-loop) "b" "a" "e" My code is: ...
4
votes
1answer
285 views

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

Is there an efficient way to evaluate something like this? ...
0
votes
3answers
914 views

Find simultaneous roots of Sin[x] and Cos[x] using FindRoot, If, and For

I am a beginner of Mathematica, I happened to come across the following exercise which may seem trivial to the experts of this field but is hard for me to do at this level. I have the equation ...
2
votes
2answers
258 views

How do I write this matrix?

The following code gives 20 data as output. How can I make a 5*4 matrix by that data list?
2
votes
3answers
147 views

Evaluating number of iteration with a certain map with While

Beeing used to programming in C-like languages I am struggling with iterations and loops with mathematica. I am trying not to use the For command, as already lots of people recommended. I am trying ...
3
votes
2answers
196 views

Correctly defining the return of a procedure

I am sort of new to mathematica and I am struggling with how to correctly set the return of a specific function which is a procedure. I am give the following task: Given the map $x_{n+1} = r x_n (1-...
0
votes
2answers
918 views

How to write if statement and for loop? [closed]

I am trying to write a function that will find a sum of all diagonal elements of matrix. I don't know how to do it correctly. This is how I tried do do it. ...
1
vote
3answers
249 views

Extracting a 2 x 2 matrix from a 9 x 9 matrix [closed]

I have the following code: For[i = 1, i <= 9, i = i + 8, For[j = 1, j <= 9, j = j + 8, Print[k[[i, j]]] ] ] The code prints a column of the ...
0
votes
1answer
1k views

Creating an array with “For” loop

I need to perform a number of operations within a For loop and store the results in an indexed array (without using the Append function) For examole (to make it ...
0
votes
2answers
758 views

Nested For loops simplified

In order to avoid a deep set of nested For loops, I came up with this method to do it with one For loop. The range of each level's For loop counting variable k1, k2, k3,... is 0 to b, and n is the ...
0
votes
1answer
615 views

ListPlot in a Loop [closed]

I have the following program which generates the plot from a data list(i.e. Data1). However I want to print some plots for some values of the parameter "a". How would I proceed to print the scatter ...
0
votes
1answer
599 views

Solve a system by Cramer's rule using loop

Consider the linear system AX=B,where A={{3, 0, 2}, {-3, 2, 2}, {2, -3, 3}},B={{3}, {-1}, {4}} and x=...
3
votes
1answer
103 views

Efficient way to evaluate numerically product sines functions

I'm trying to perform integration of the following matrix. ...
4
votes
2answers
124 views

Plotting Basis Order function

Does anyone know how could I plot the basis order functions for the following function (code below)? An example of what I should expect is https://en.wikipedia.org/wiki/Lagrange_polynomial#/media/...
1
vote
2answers
107 views

What is wrong with the Mann iteration?

I have written the Mann iteration's code for the mapping $\frac{z}{1+z^2}$, in Mathematica, but it is very slow. In fact it needs more than 200 seconds for obtaining 17th iteration, while Matlab does ...
3
votes
1answer
293 views

How to `ParametricPlot` a system of ODEs for a set of initial conditions?

I want to ParametricPlot the solution to a differential equation for which an initial value ranges from 1 to 10. The problem (I believe) is that I don't know how ...
2
votes
2answers
85 views

How to automatically change plotting options for an array of plots?

I'm working with an array of plots. I'd like to change the color and the legend for each of the plots in my array. For example, working with 10 plots: I generate the empty array of plots ...
1
vote
2answers
178 views

How can I combine the output of a `For` loop into a list?

I am currently writing on a code to get RandomNumbers which are geometrical distributed. But i need the output in one list because i want to plot the output Can anyone help me? Here is the current ...
0
votes
1answer
282 views

Chi square minimisation wrt variables within an integration?

I'm trying to fit a model curve to some data by performing a chi square minimisation wrt three parameters $a,b$ and $NN$. The trouble I am having is that the variables with which I want to minimise ...
0
votes
2answers
127 views

Exporting result of calculation to file at each step of iterative routine

I am performing a calculation that involves a triple "For" command. My goal is to export the table of all values of some function (which depends on the three indices) to a data file for statistical ...
1
vote
1answer
267 views

Multiple file export

Here my code is running fine.In this code I put x=1 but I want to run x from 1 to 100 and for each value of x I want to export the excel file by the name according to x value.like for x=1 it export by ...
0
votes
1answer
245 views

Parallelizing nested for loops

I'm currently computing nested for loops. I count up a few counting variables and compute a value for each set of counting variables, then that value is added to an array. It is a way of adding up ...
0
votes
1answer
71 views

Quick Wolfram Question [closed]

This is my code so far: For[i=2, i<10, i++, Solve[x^i == x+1 && x >0]] I would like to sum up all the x values that it outputs. How can I do so?
1
vote
1answer
88 views

Optimizing a slow For loop

I know that this is a "beginner question", and, in fact, I am. I want to improve my code, as it takes really too much time to run. I have already read some other discussions, like here, but ...
1
vote
2answers
87 views

For loop not working as expected from a C programmer's perspective [closed]

Why does Print[n] produce output inside a For loop, while Sqrt[n] (as well as many other ...

15 30 50 per page