Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica - Construct a Matrix

  1. Feb 7, 2010 #1
    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.
     
  2. jcsd
  3. Feb 7, 2010 #2

    Dale

    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
     
  4. Feb 7, 2010 #3
    thank you, yes you are right, to apply a condition, i need to put ;

    thank you again.
     
  5. Feb 7, 2010 #4
    I have tried out your code and oddly it doesn't work on me at all :redface:
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook