Matrix Handling in Mathematica

In summary, the conversation discusses running a Mathematica program that creates a matrix, and how changing the program affects the output. The person is having trouble clearing the memory from the previous run. Suggestions are given to use ClearAll or Remove instead of Clear to solve the issue.
  • #1
Rjah
1
0
Hi,

I ran this program:

P = Table[Subscript[p, i, j], {i, 0, 3}, {j, 0, 2}]
For[j = 0; i=0, j <= 2, j++, Subscript[p, i, j] = 2]
MatrixForm[P]

What is gives me is this:
2 2 2
p[1,0] p[1,1] p[1,2]
p[2,0] p[2,1] p[2,2]
p[3,0] p[3,1] p[3,2]


If I change the program and rerun it:

Clear[p]
P = Table[Subscript[p, i, j], {i, 0, 3}, {j, 0, 2}]
For[j = 0; i=1, j <= 2, j++, Subscript[p, i, j] = 2]
MatrixForm[P]

Now it gives me:

2 2 2
1 1 1
p[2,0] p[2,1] p[2,2]
p[3,0] p[3,1] p[3,2]


I was expecting something like:
p[0,0] p[0,1] p[0,2]
1 1 1
p[2,0] p[2,1] p[2,2]
p[3,0] p[3,1] p[3,2]


I want mathematica to erase its memory from the previous run, but nothing seems to work!


Any suggestions?
 
Physics news on Phys.org
  • #2
Did you try something like
Clear[P]
or
For[every {i, j}, Subscript[p, i, j] = .;]
 
  • #3
You might try ClearAll or Remove instead of Clear.
 

1. What is matrix handling in Mathematica?

Matrix handling in Mathematica refers to the ability of the software to perform various operations on matrices, which are rectangular arrays of numbers or symbols. These operations include matrix multiplication, addition, inversion, and diagonalization.

2. How do I create a matrix in Mathematica?

To create a matrix in Mathematica, you can use the Array or Table functions. For example, to create a 3x3 matrix of random numbers, you can use the command Array[RandomReal[], {3,3}]. You can also directly input the elements of the matrix using the Table function, such as Table[i+j, {i, 1, 3}, {j, 1, 3}] to create a 3x3 matrix with elements ranging from 2 to 6.

3. How do I perform matrix operations in Mathematica?

In Mathematica, you can perform various matrix operations using built-in functions such as Dot for matrix multiplication, Plus for addition, Inverse for matrix inversion, and Eigensystem for diagonalization. You can also use the infix notation, such as A.B for Dot[A,B], to make the code more readable.

4. Can I manipulate individual elements in a matrix in Mathematica?

Yes, you can access and manipulate individual elements in a matrix using the Part function. For example, to change the value of the element in the second row and third column of a matrix A, you can use the command A[[2,3]] = 5. You can also use Part to extract specific rows or columns from a matrix.

5. How do I visualize a matrix in Mathematica?

You can visualize a matrix in Mathematica using the MatrixPlot function, which creates a color-coded plot of the matrix. You can also use ArrayPlot to visualize the matrix as a grid of colored squares, where the color represents the value of each element. Additionally, you can use the ListDensityPlot function to create a plot of the matrix values as a function of their position in the matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Topology and Analysis
Replies
26
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
0
Views
255
  • Calculus and Beyond Homework Help
Replies
3
Views
296
Back
Top