Subtracting column values in Matlab tables

Hi All, I have a table in matlab with 84 rows and 3 columns. I want to subtract values in column 3 from column 2. How do I do that. Thanks,

 Accepted Answer

YourTable{:,2} - YourTable{:,3}
assuming that they are the same data type.
(EDIT: corrected a minor typo in the first term)

More Answers (2)

the cyclist
the cyclist on 10 Mar 2018
Edited: the cyclist on 10 Mar 2018
The exact syntax will depend a little on how your table was created. Here are a couple examples.
N = 84;
v1 = rand(N,1);
v2 = rand(N,1);
v3 = rand(N,1);
T = table(v1,v2,v3);
diffT = T.v2 - T.v3;
or
T = table(rand(84,3));
T.Var1(:,2) - T.Var1(:,3)
If you are using the word "table" loosely -- meaning you actually just have a numeric array and not a table data type, then you can just do
T = rand(84,3);
diffT = T(:,2) - T(:,3);

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!