Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 731

Questions about Mathematica's procedural programming paradigm.

10 votes

How do I achieve 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: r = 0; Do[Catch[ If[EvenQ[i], Throw[Null]]; r += i ], {i, 10}]; r (* Out: 25 *) (Example taken from the do …
C. E.'s user avatar
  • 71.2k
4 votes
Accepted

Changing iteration variable in Do loop

This is one the cases where For can be helpful (generally it's just more complicated.) With For, you can manipulate the iteration variable, which you cannot do with Do. Example: For[ i = 1, i <= 4 …
C. E.'s user avatar
  • 71.2k
3 votes
Accepted

How to use syntax instead of double for loop

If I understand it correctly, you're looking to compute the distance matrix of those two sets of vectors. This can be done, as Jason says, using Outer. It can also be done using the built-in function …
C. E.'s user avatar
  • 71.2k
1 vote

Skip time taking loop values to take input next value after certain time

Here is an example of MarcoB's suggestion: AbsoluteTiming[ TimeConstrained[Print[Pause[1]; 1], 3]; TimeConstrained[Print[Pause[5]; 10], 3]; TimeConstrained[Print[Pause[2]; 100], 3]; ] You can thi …
C. E.'s user avatar
  • 71.2k
3 votes

Better to use loop or function when populated list?

Like this: computeEffect[xEffect_] := With[{offset = RandomInteger[{1, 10}]}, If[xEffect - offset < 1, RandomValue, randomA[[xEffect - offset]] + k randomB[[xEffect - offset]] ] ] effec …
C. E.'s user avatar
  • 71.2k
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 {3, 12, 27, 48, 1, 1, 1, 1, 243, 300, …
C. E.'s user avatar
  • 71.2k