Nested for and lists in mathematica

  • Thread starter shakespeare86
  • Start date
  • Tags
    Mathematica
Additionally, you should use the AppendTo function instead of Append, as it is more efficient when working with lists. In summary, the problem in the code is that the variable t is not defined outside of the inner For loop, causing an error when trying to append it to the list l. The solution is to define t before the loop and update its value with the new random one each time using AppendTo.
  • #1
shakespeare86
21
0
Hello, I'm new using Mathematica and actually can't understand what's the problem in this code

Code:
l = {}

For[k = 1, k < 3,
 For[j = 1, j < 3, 
    t1 = RandomInteger[{1, 10}];
    t2 = RandomInteger[{1, 10}];
    t = (t1 + t2)/2;
    j++;
    ]
   l = Append[l, t];
 k++;
 ]

The error I get is

Set::write: Tag Times in Null {} is Protected. >>

I've also tried to use an array or a table in the place of the list l, but the error persist.
It's like if after the nested for, I couldn't append elements to the list or add elements to an array.

Have you any idea to do what I would like to do?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
The problem here is that the variable t is not defined outside of the inner For loop. The value of t is reset to a new random value each time the inner loop is executed, so when you try to use it outside of the loop, it has no value. The easiest way to fix this is to define t before the inner For loop, and then just update its value each time with the new random one.
 

FAQ: Nested for and lists in mathematica

1. What is the purpose of using nested for loops and lists in Mathematica?

Nested for loops and lists are used in Mathematica to perform repetitive tasks or to organize and manipulate large amounts of data. They allow for efficient processing of data and can be used in various mathematical and scientific applications.

2. How do I create a nested list in Mathematica?

To create a nested list in Mathematica, you can use the Table or Array function. For example, Table[i*j, {i, 1, 3}, {j, 1, 3}] will create a 3x3 nested list with the elements being the product of the indices i and j.

3. Can I use if statements inside a nested for loop in Mathematica?

Yes, you can use if statements inside a nested for loop in Mathematica. This allows for conditional execution of code based on certain criteria. For example, you can use an if statement to check if a certain element in a nested list meets a specific condition and perform a task based on the result.

4. How do I access elements in a nested list in Mathematica?

To access elements in a nested list in Mathematica, you can use double brackets [[ ]] notation. The first set of brackets indicates the row and the second set indicates the column. For example, if myList is a nested list, myList[[2,3]] will access the element in the second row and third column.

5. Can I use nested for loops and lists in solving mathematical problems in Mathematica?

Yes, nested for loops and lists are commonly used in solving mathematical problems in Mathematica. They allow for efficient handling of large datasets and can be used to perform calculations and manipulate data in a structured manner.

Similar threads

Replies
1
Views
1K
Replies
4
Views
4K
Replies
21
Views
2K
Replies
6
Views
4K
Replies
2
Views
4K
Replies
10
Views
2K
Replies
3
Views
2K
Replies
10
Views
2K
Back
Top