Skip to main content
added 316 characters in body
Source Link

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

Additional question 1) You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

If you want to use indices anyway, you can do it with Table:

f1 = Table[
  (g[[k]] h[[k]])/Sum[g[[i]] h[[i]], {i, Length[g]}],
  {k, Length[g]}]

Additional question 2) A blue variable means it is unbound to a value. The variable will be used as a symbol instead. Teal color means it is bound to a value via an enclosing function. In your example, Sum binds i to a value it fills in. A black variable has a globally assigned value.

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

If you want to use indices anyway, you can do it with Table:

f1 = Table[
  (g[[k]] h[[k]])/Sum[g[[i]] h[[i]], {i, Length[g]}],
  {k, Length[g]}]

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

Additional question 1) You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

If you want to use indices anyway, you can do it with Table:

f1 = Table[
  (g[[k]] h[[k]])/Sum[g[[i]] h[[i]], {i, Length[g]}],
  {k, Length[g]}]

Additional question 2) A blue variable means it is unbound to a value. The variable will be used as a symbol instead. Teal color means it is bound to a value via an enclosing function. In your example, Sum binds i to a value it fills in. A black variable has a globally assigned value.

added Table example
Source Link

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

If you want to use indices anyway, you can do it with Table:

f1 = Table[
  (g[[k]] h[[k]])/Sum[g[[i]] h[[i]], {i, Length[g]}],
  {k, Length[g]}]

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

If you want to use indices anyway, you can do it with Table:

f1 = Table[
  (g[[k]] h[[k]])/Sum[g[[i]] h[[i]], {i, Length[g]}],
  {k, Length[g]}]
added 160 characters in body
Source Link

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

Create lists like this:

g = {1, 2, 3, 4}
h = {10, 20, 30, 40}

Then you can sum like this:

Sum[g[[i]] h[[i]], {i, Length[g]}]

or more nicely (Mathematica does element-wise operations automatically where applicable):

Total[g*h]

You can use this feature to easily calculate a new list with the results, without using indices:

f = (g h)/Total[g h]

{1/30, 2/15, 3/10, 8/15}
Source Link
Loading