Mathematica Mathematica - Construct a Matrix

AI Thread Summary
The discussion revolves around constructing a specific m x m matrix with defined values on its diagonal, lower triangular, and upper triangular parts. The user initially attempted to define a function f to generate the matrix but encountered an issue where the output was a matrix filled with 'True' values. The problem was identified as a misdefinition of the function f, where the logical conditions were incorrectly applied using division instead of conditions. A suggestion was made to correct the function definitions by using conditional statements (using /;) to achieve the desired matrix structure. Despite the correction, the user reported that the revised code still did not work as expected, indicating ongoing challenges in implementing the solution.
sugaku
Messages
16
Reaction score
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
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
 
thank you, yes you are right, to apply a condition, i need to put ;

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

Similar threads

Replies
4
Views
4K
Replies
13
Views
4K
Replies
1
Views
5K
Replies
4
Views
2K
Replies
1
Views
6K
Back
Top