Define Size of Matrix Piece by Piece in Loop: Mathematica Help

  • Context: Undergrad 
  • Thread starter Thread starter brydustin
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on defining the size of a matrix in Mathematica by creating a zero matrix and adding elements to it in a loop. The user encountered an error message indicating that the symbol U does not have an immediate value when attempting to assign a value to a specific element in the matrix. The solution provided suggests initializing the matrix without using the MatrixForm wrapper, as it interferes with the assignment operation. The correct initialization should be done using U = ConstantArray[0, {3, 3}] to avoid the error.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of matrix operations in Mathematica
  • Knowledge of how to manipulate arrays in Mathematica
  • Basic understanding of error handling in programming
NEXT STEPS
  • Explore the use of ConstantArray for matrix initialization in Mathematica
  • Learn about the differences between matrix representation and display functions like MatrixForm
  • Investigate error messages in Mathematica and how to resolve them
  • Study advanced matrix manipulation techniques in Mathematica
USEFUL FOR

Mathematica users, data scientists, and programmers who are working with matrix operations and seeking to understand error handling in array manipulations.

brydustin
Messages
201
Reaction score
0
Trying to define the size of a matrix by making a zero matrix then adding elements to it (piece by piece in a loop).

Here is the general idea:

U = ConstantArray[0, {3, 3}] // MatrixForm
U[[2, 2]] = 2

(except in the actual code, I would use U[[i,j]] , for example).
My error message is:
Set::noval: Symbol U in part assignment does not have an immediate value. >>

I suspect that it is treating it as a tensor but why? (i.e. U[[2,2]] assigns a value to the second dimension of the tensor, and to the second row (without specifiing column)).
But why? If I check the dimension I get the empty set!
Dimensions
output = {}
what's going on here?
 
Physics news on Phys.org
You might want to try posting this in the Math forum if you don't get any help here. :smile:
 
brydustin said:
Trying to define the size of a matrix by making a zero matrix then adding elements to it (piece by piece in a loop).

Here is the general idea:

U = ConstantArray[0, {3, 3}] // MatrixForm
U[[2, 2]] = 2

Try just U = ConstantArray[0, {3, 3}] without the //MatrixForm. If you want to view in MatrixForm, say MatrixForm[U = ConstantArray[0, {3, 3}]] or (U = ConstantArray[0, {3, 3}])//MatrixForm

(I don't know why, = precedes // or something)
 
Last edited:

Similar threads

  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 78 ·
3
Replies
78
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K