I want to construct a big matrix where the input of the matrix element is based on the relation between the row indices and column indices.
Here's an example of what I tried but did not work:
f[n_] := n
g[m_] := m
W[n_, m_] /; f[n] == g[m] := 1
W[n_, m_] /; f[n] != g[m]: = 2
Table[W[n, m], {n, 1, 4}, {m, 1, 4}]
I also tried the following (using the variable n on the LHS instead of f[n]), and it worked.
f[n_] := n
g[m_] := m
W[n_, m_] /; n == g[m]: = 1
W[n_, m_] /; n != g[m] := 2
Table[W[n, m], {n, 1, 4}, {m, 1, 4}]
But that's not what I want since the relation between the indices is a bit complicated.
Is there any way to put a condition with a function on the LHS?
fandg? Moreover, would you please explain what exactly does not work? – Henrik Schumacher Mar 21 '19 at 10:30Array[W, {4, 4}]instead ofTable[W[n,m],{n,1,4},{m,1,4}]. – Henrik Schumacher Mar 21 '19 at 10:33n=4; ConstantArray[1, {n, n}] + Unitize[Outer[Subtract, f /@ Range[n], g /@ Range[n], 1]]. – Henrik Schumacher Mar 21 '19 at 10:37Clear[f, g, n, m, W]and then reevaluating your input. – Carl Woll Mar 21 '19 at 15:43