0
$\begingroup$

I have a function which can be represented as a weighted sum of parameters f(m, n). Where m & n have values 1 - 10.

F = A1* f(1, 2) + A2* f(2, 3) + A3* f(3, 1) + .... 

I want to use this F to form a 2D table such that

T = Table[F, {i, 1, 10}, {j, 1, 10}]

with conditions such that for (i, j)th element of table

If i = m && j = n -> f(m, n) = 5, 
If i = n && j = m -> f(m, n) = -5
otherwise f(m, n) = 0

To elaborate, for the {2, 3}th element of this table, f(2, 3) should be replaced by 5, f(3, 2) by -5 and other f terms by 0.

How can I do it. Could If function be used with table ?? I could not make it work. Will appreciate any suggestion.. Thanks

$\endgroup$
6
  • $\begingroup$ may be I am missing something. But where does m and n come from in the code you show? And what is f(f(m, n)) $\endgroup$
    – Nasser
    Commented Sep 25, 2017 at 19:35
  • $\begingroup$ f(m, n) is a general representation of components f(1, 2), f(2, 3) etc. Hope it explains... $\endgroup$
    – user49535
    Commented Sep 25, 2017 at 19:55
  • $\begingroup$ but what is n and m in the code? You show i and j only. Are n and m separate input? $\endgroup$
    – Nasser
    Commented Sep 25, 2017 at 19:56
  • $\begingroup$ so in this example you just typed, is m=1 and n=2 ? If so, please edit your question and clarify that n and m are extra input. $\endgroup$
    – Nasser
    Commented Sep 25, 2017 at 20:04
  • $\begingroup$ Yes.. Let me simplify. Lets say f = f(1, 2) + f(1, 3) + f(2, 3). I want to make a 2D table with f such that indices i = 1 - 3 and j = 1 - 3. Now each element of table will represented by a {i, j} set. What I need is that for the element represented by i = 1; j = 2 f(1, 2) = 5 and so on.. $\endgroup$
    – user49535
    Commented Sep 25, 2017 at 20:05

1 Answer 1

1
$\begingroup$

To elaborate, for the {2, 3}th element of this table, f(2, 3) should be replaced by 5, f(3, 2) by -5 and other f terms by 0.

I do not know if this what you want or not

ClearAll[f,i,j]
f[i_,j_]/;i<j:=5
f[i_,j_]/;j<i:=-5
f[i_,j_]:=0
(T0=Table[f[i,j],{i,1,10},{j,1,10}])//MatrixForm

Mathematica graphics

Add per comment below

But different constants multiplied to f parameters in F should make sure that elements of matrix are multiple of 5 & -5, not 5 and -5.

To multiply each entry by A constant, you need to provide the corresponding A constants to use. Here is an example, using random generated constants.

ClearAll[f,i,j]
f[i_,j_]/;i<j:=5 * A0[[i,j]]
f[i_,j_]/;j<i:=-5*A0[[i,j]]
f[i_,j_]:=0
A0=RandomInteger[10,{10,10}];
(T0=Table[f[i,j],{i,1,10},{j,1,10}])//MatrixForm

Mathematica graphics

Update Ref comment

Here is a much easier example and expected o/p for F= 5*f(1, 3)+6*f(2, 3)+4*f(1, 2) T = Table[F, {i, 1, 3}, {j, 1, 3}]; Should give T = {{ 5*0+6*0+4*0, 5*0+6*0+4*5, 5*5+6*0+4*0}, { 5*0+6*0+4*(-5), 5*0+6*0+4*0, 5*0+6*5+4*0}, { 5*(-5)+6*0+4*0, 5*0+6*(-5)+4*0, 5*0+6*0+4*0}} on simplification – T = {{0, 20, 25}, {-20, 0, 30}. {-25, -30 0}}

Ok, may be this is what you want now? You need a way to specify the constants and the entries they affect. I am still not sure if this is what you want

ClearAll[i,j]

(*this A0 encodes the input:  5*f(1, 3)+6*f(2,3)+4*f(1, 2) *)
A0={{5,{1,3}},{6,{2,3}},{4,{1,2}}};

T0=Table[0,{i,1,3},{j,1,3}];
makeEntry[{A0_,{i_,j_}}]:=
      (T0[[i,j]]=If[i<j,5*A0,If[i>j,-5*A0,0]];T0[[j,i]]=-T0[[i,j]]);

makeEntry[#]&/@A0;
MatrixForm[T0]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Let me understand it, could be a way forward. But different constants multiplied to f parameters in F should make sure that elements of matrix are multiple of 5 & -5, not 5 and -5. $\endgroup$
    – user49535
    Commented Sep 25, 2017 at 20:35
  • $\begingroup$ @user49535 I added the constants. $\endgroup$
    – Nasser
    Commented Sep 25, 2017 at 20:44
  • $\begingroup$ Here is a much easier example and expected o/p for F= 5*f(1, 3)+6*f(2, 3)+4*f(1, 2) T = Table[F, {i, 1, 3}, {j, 1, 3}]; Should give T = {{ 5*0+6*0+4*0, 5*0+6*0+4*5, 5*5+6*0+4*0}, { 5*0+6*0+4*(-5), 5*0+6*0+4*0, 5*0+6*5+4*0}, { 5*(-5)+6*0+4*0, 5*0+6*(-5)+4*0, 5*0+6*0+4*0}} on simplification – T = {{0, 20, 25}, {-20, 0, 30}. {-25, -30 0}} $\endgroup$
    – user49535
    Commented Sep 25, 2017 at 20:54
  • $\begingroup$ Thanks. if you look this easier ex, for {1, 1} element of matrix - f(1, 3) = f(2, 6) = f(1, 2) = 0. For {1, 2}th element only f(2, 1) = -5, others are 0. For {1, 3}th element f(1, 3) = 5 while others are 0..and so on... $\endgroup$
    – user49535
    Commented Sep 25, 2017 at 21:00
  • $\begingroup$ Thanks, but seems I am not able to explain the problem properly. It is not related to setting different value for f if indices m>n, n> m and n = m. It is about setting different values to a variable in different elements of a table. Let me try with even easier example. Lets take F = 24 + x. Now if I use Table[F, {i, 1, 3}]. O/P will be {24+x, 24+x, 24+x}. What I want is to give different independent values to x for i = 1, 2 and 3. For ex- If[i == 1, x = 2, If[i == 2, x = 4, If[i == 3, x = 5, 0]]]. Hence the output will be {24+2, 24+4, 24+5} or {26, 28, 29}. Appreciate all help. $\endgroup$
    – user49535
    Commented Sep 26, 2017 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.