Skip to main content
24 votes

How to pass variables by reference and by value

You asked for a general explanation instead of just focusing on specific application examples, so here it goes ... The concepts of "pass by reference" and "pass by value" that you ...
Szabolcs's user avatar
  • 235k
20 votes
Accepted

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

Okay, let's focus on this snippet: ...
Henrik Schumacher's user avatar
14 votes

C++' "do ... while" loop equivalent in Mathematica

Version 13.1 introduced the function Until Until[test,body] evaluates body and then test, repetitively, until test first gives True.
Jason B.'s user avatar
  • 68.7k
11 votes

All solutions that satisfy $ x_{1}^{5}+x_{2}^{5}+x_{3}^{5}+x_{4}^{5}-x_{5}^{5}=0 $

I am a bit surprised that not a Mathematica solution with a statement "I don't think Mathematica is suited for this kind of problem." is accepted. I do believe that Mathematica is well ...
yarchik's user avatar
  • 18.7k
10 votes

How do I achive the function of Continue using Catch and Throw?

If you wrap the body of the loop with Catch, then any Throw will act as Continue: ...
C. E.'s user avatar
  • 70.7k
10 votes
Accepted

Improve speed with a double for loop

Even in MATLAB, this would not be good programming style because successive concatenation is awfully slow. (And for is slow, too.) Better use ...
Henrik Schumacher's user avatar
10 votes
Accepted

Implementing "do this, then return that" in function definitions

If you want, you could write some thing like this f[x_] := Module[{k = x}, Do[k = k^2 + 1, {i, 0, x - 1}]; k] A more natural form would be ...
mikado's user avatar
  • 16.8k
10 votes

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

Here's a simpler version: SetAttributes[Until,HoldAll] Until[body_, test_] := While[body; !test]
Carl Woll's user avatar
  • 131k
10 votes
Accepted

All solutions that satisfy $ x_{1}^{5}+x_{2}^{5}+x_{3}^{5}+x_{4}^{5}-x_{5}^{5}=0 $

I don't think Mathematica is suited for this kind of problem. Here's a C code that can solve it in 10 seconds on my laptop: ...
Roman's user avatar
  • 48k
9 votes
Accepted

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

UPDATE: After reposting the below function after modifications from the deleted post to make it work after OP question was changed, I determined a way to utilize ideas from the original deleted post's ...
ciao's user avatar
  • 25.8k
9 votes

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

If you introduce a length bound already, there is absolutely no reason for Append/AppendTo. Such a loop with ...
Henrik Schumacher's user avatar
9 votes
Accepted

Replace elements of a list based on a function

To answer your question directly, a loop could be written like this: f = Table[3*k*k, {k, 1, 16}]; Do[ f[[4 i + j]] = 1; , {i, {1, 3}}, {j, 1, 4} ]; f {...
C. E.'s user avatar
  • 70.7k
9 votes
Accepted

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

I tend to use While[True, body; If[condition, Break[]] ] but what you show is equally good. This is just an alternative. Try to avoid procedural loops in ...
Szabolcs's user avatar
  • 235k
8 votes

How to pass variables by reference and by value

You need: SetAttributes[myfunc, HoldAll] You then have: ...
user21's user avatar
  • 39.9k
8 votes
Accepted

Problem with Excel operation in NETLink way

In the Excel object model, the worksheet symbol Cells represents a property, not a method. In Visual Basic and other languages, many properties have a default ...
WReach's user avatar
  • 69k
8 votes
Accepted

While loop in a function

In addition to the very good suggestions by Fraccalo, the issue here is that Mathematica refuses to overwrite arguments of functions (in general). So you have to introduce a local variable like this: ...
Henrik Schumacher's user avatar
8 votes

Improve speed with a double for loop

(Subtract @@@ Subsets[Most@x, {2}])^2 + (Subtract @@@Subsets[Most@y, {2}])^2 will produce precisely the output of your OP with appropriate performance. Since your ...
ciao's user avatar
  • 25.8k
8 votes
Accepted

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

Update: A faster alternative: ...
kglr's user avatar
  • 396k
8 votes

Implementing Newton's Method?

The main reason is that x1 is not assigned a value before entering the for loop: ...
A little mouse on the pampas's user avatar
8 votes

Implementing Newton's Method?

Worth putting it out there that the For loop is not your friend. This does the same ...
b3m2a1's user avatar
  • 46.9k
8 votes
Accepted

how can i get the following figure using the for loop

Welcome to MMA SE! What the code above does is simply repeatedly overwrite the value of A. In each iteration of the loop, it evaluates ...
thorimur's user avatar
  • 9,020
8 votes
Accepted

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

First of all, the correct commands are Break[] and Continue[], you are missing the square brackets. Secondly, you do not need <...
Domen's user avatar
  • 26.3k
8 votes
Accepted

How to put a FOR loop in a WHILE loop

My answer is, "Don't". Create CAC. ...
Edmund's user avatar
  • 42.3k
8 votes

Monitoring Table or Do with Print

Monitor[Table[Pause[0.1], {i, 0, 100}], i]
Roman's user avatar
  • 48k
8 votes
Accepted

Perform same operation along dimension of an array

...
xzczd's user avatar
  • 66.8k
8 votes
Accepted

A loop in which three indices are changed

Table ClearAll[a] Table[a[i, j] = Partition[LL, 3][[i, j]], {i, 1, 2}, {j, 1, 3}]; {a[1, 3], a[2, 2], a[2, 3]} {3, 5, 4} <...
kglr's user avatar
  • 396k
7 votes
Accepted

Creating a multiline function with localized variables

You should write your function like so: myFunction[x_] := Module[{y}, y = x; y = y + 3; (y + 3)^3] Note the underscore in ...
m_goldberg's user avatar
  • 108k
7 votes
Accepted

Populating function inputs from lists

f@@(list1 ~Join~ list2) Or, more generally, use @@ to "open" the structure of List: <...
Slepecky Mamut's user avatar
7 votes
Accepted

Evaluating number of iteration with a certain map with While

The problem with your code is that for some values of c, Abs[z] will never become larger than 2. You need to cap the number of ...
Szabolcs's user avatar
  • 235k
7 votes

How can I solve the Ramanujan problem by using Wolfram language?

...
Bob Hanlon's user avatar
  • 159k

Only top scored, non community-wiki answers of a minimum length are eligible