Mathematica - Construct a Matrix

  • Context: Mathematica 
  • Thread starter Thread starter sugaku
  • Start date Start date
  • Tags Tags
    Mathematica Matrix
Click For Summary

Discussion Overview

The discussion revolves around constructing a specific m x m matrix in Mathematica, where the diagonal elements are 1/2, the lower triangular elements are zeros, and the upper triangular elements are ones. Participants are exploring the correct implementation of the matrix construction function and troubleshooting issues with the output.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares an initial attempt to define a function f for constructing the matrix, but reports that the output is incorrect, resulting in a matrix filled with 'True' values.
  • Another participant identifies the issue in the function definition, explaining that the use of division by logical statements leads to unintended results, and suggests using conditions instead.
  • A third participant acknowledges the correction and agrees that applying conditions is necessary.
  • However, a fourth participant expresses that the suggested code does not work for them, indicating potential issues with implementation or context.

Areas of Agreement / Disagreement

While there is agreement on the need to use conditions in the function definition, there remains disagreement or uncertainty regarding the effectiveness of the proposed solution, as one participant reports that it does not work for them.

Contextual Notes

Participants have not resolved the issue of why the suggested code fails for one user, indicating possible differences in their environments or additional factors affecting the implementation.

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 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
39
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K