5

I have a org table:

| a | b | c |
|---+---+---|
| 1 | 2 | i |
| 3 | 4 | j |
|---+---+---|
| 4 | 6 |   |

I want to multiply columns a and b and sum them up (except last row: 1*2+3*4) and put into the last cell:

| a | b | c |
|---+---+---|
| 1 | 2 | i |
| 3 | 4 | j |
|---+---+---|
| 4 | 6 | 14|

What is the formula to achieve this? I tried #+TBLFM: @>$>=sum($1 * $2) without luck.

My org version: Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /Applications/Emacs.app/Contents/Resources/lisp/org/)

1
  • 1
    This is the scalar product of two vectors. Emacs calc (which is used by the table code in Org mode) can calculate the scalar product of two vectors using *, so the trick is to figure out how to specify the two vectors. @gigiair's answer shows you how to do that.
    – NickD
    Commented Feb 22, 2020 at 16:14

2 Answers 2

8
 @4$3=(@2$1..@3$1)*(@2$2..@3$2)

or

 @>$>=(@<<$<..@>>$<)*(@<<$<<..@>>$<<)

or

 @>$>=(@I$1..@II$1)*(@I$2..@II$2)

using the horizontal lines to delimit instead of explicit row numbers.

The relevant section of the manual is dense but well-worth reading (and re-reading).

1
  • 1
    parentheses are not mandatory, but make the formula easyer to read.
    – gigiair
    Commented Feb 22, 2020 at 8:19
0

You could use the Orgtbl-Aggregate package available on Melpa

The sums are in a separate table, easy to update

#+name: mytable
| a | b | c |
|---+---+---|
| 1 | 2 | i |
| 3 | 4 | j |

#+BEGIN: aggregate :table "mytable" :cols "vsum(a) vsum(b) vsum(a*b)"
| vsum(a) | vsum(b) | vsum(a*b) |
|---------+---------+-----------|
|       4 |       6 |        14 |
#+END:

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.