Mathematica - Construct a Matrix

In summary, the problem is that you are trying to define f as an equation, when it should be a function.
  • #1
sugaku
17
0
Good day to all,
I'm trying to construct a m x m (size) matrix which have 1/2 on the diagonal, zeros to the lower triangular and 1 to the upper triangular.

m=10;
f[i_,j_]:=1/2 /i==j
f[i_,j_]:=1 /i>=j
f[i_,j_]:=0 /i<j
m=Array[f,{m,m}];
m //MatrixForm

I think, supposedly this will works but the answer came out with a matrix 'True'


{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"},
{"True", "True", "True", "True", "True", "True", "True", "True",
"True", "True"}

Anybody could enlightened me, please.

thank you in advance.
 
Physics news on Phys.org
  • #2
The problem is in your definition of f. If you execute
?f
then you will see that the only definition for f is
f[i_,j_]:=(0/i)<j

Then, for e.g. i=2 and j=3 this expression reduces to (0/2)<3 which evaluates to True. The expression is indeterminate for i=0 and otherwise is True for j>0 and False otherwise.

What I believe you want to do is to apply a Condition using /; to your definitions of f rather than division by a logical statement using /

Try:
f[i_, j_] := 1/2 /; i == j
f[i_, j_] := 1 /; i >= j
f[i_, j_] := 0 /; i < j
 
  • #3
thank you, yes you are right, to apply a condition, i need to put ;

thank you again.
 
  • #4
I have tried out your code and oddly it doesn't work on me at all :redface:
 
  • #5


Hello, it seems that there may be an error in your code. Instead of using "==" in your function, try using "=" to assign the values for the upper and lower triangular parts of the matrix. Additionally, you can use the built-in function "DiagonalMatrix" to easily create a diagonal matrix with a specified value on the diagonal. The corrected code may look something like this:

m = 10;
upper = 1;
lower = 0;
diagonal = 1/2;
m = DiagonalMatrix[Table[diagonal, {m}]] + UpperTriangularize[Table[upper, {m}, {m}], 1] + LowerTriangularize[Table[lower, {m}, {m}], -1];
m // MatrixForm

This should give you a matrix with 1/2 on the diagonal, 1 on the upper triangular, and 0 on the lower triangular. I hope this helps!
 

What is Mathematica and how is it used for constructing matrices?

Mathematica is a software program that is widely used in scientific and mathematical fields. It has a built-in functionality for constructing matrices, which are arrays of numbers or symbols arranged in rows and columns.

How do I create a matrix in Mathematica?

To create a matrix in Mathematica, you can use the command "MatrixForm" followed by the list of elements in the matrix enclosed in curly braces. For example, the command "MatrixForm[{{1,2,3},{4,5,6}}]" will create a 2x3 matrix with the elements 1, 2, 3 in the first row and 4, 5, 6 in the second row.

Can I perform operations on matrices in Mathematica?

Yes, Mathematica has a wide range of built-in functions for performing operations on matrices, such as addition, multiplication, and inversion. You can also write your own custom functions to perform specific operations on matrices.

How can I access specific elements in a matrix in Mathematica?

To access a specific element in a matrix, you can use the double bracket notation. For example, to access the element in the second row and third column of a matrix called "A", you would use the command "A[[2,3]]".

Can I use Mathematica to solve systems of equations involving matrices?

Yes, Mathematica has a built-in function called "LinearSolve" that can be used to solve systems of equations involving matrices. You can also use other functions such as "Solve" or "NSolve" depending on the complexity of the equations.

Similar threads

  • Math Proof Training and Practice
Replies
19
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
0
Views
2K
  • Introductory Physics Homework Help
2
Replies
39
Views
2K
  • Precalculus Mathematics Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Quantum Physics
Replies
3
Views
790
  • Programming and Computer Science
Replies
13
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top