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
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
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: