# Mathematica - Construct a Matrix

1. Feb 7, 2010

### sugaku

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"}

2. Feb 7, 2010

### Staff: Mentor

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. Feb 7, 2010

### sugaku

thank you, yes you are right, to apply a condition, i need to put ;

thank you again.

4. Feb 7, 2010

### Dolphiney

I have tried out your code and oddly it doesn't work on me at all