Calculations in table based on variable names
2 views (last 30 days)
Show older comments
I am trying to find a better solution to calculation using data stored in table. I have a large table with many variables (100+) from which I select smaller sub-table with only two observations and their difference for smaller selection of variables. Thus, the resulting table looks for example similarly to this:
AIR BBS BRI
_________ ________ _________
test1 12.451 0.549 3.6987
test2 10.2 0.47 3.99
difference 2.251 0.078999 -0.29132
Now, I need to multiply the ‘difference’ row with various coefficients that differ between variables. I can get the same result with the following code:
T(4,:) = array2table([T.AIR(3)*0.2*0.25, T.BBS(3)*0.1*0.25, T.BRI(3)*0.7*0.6/2])
However, I need more flexible solution since the selection of variables will differ between applications. I was thinking that better solution might be using varfun and function with if elseif (applying coefficients based on variable names):
T(4,:) = varfun(func,T3(3,:),'InputVariables',{'AIR' 'BBS' 'BRI'})
But I was unable to figure out how to correctly express the function that would assign the proper equations based on variables names. Any help would be appreciated. Thank you guys.
0 Comments
Answers (2)
Jian Wei
on 21 Jul 2014
You can try the following. First, define a function in a file named func.m that contains the code below.
function [air_out, bbs_out, bri_out] = func(air, bbs, bri)
air_out = air*0.2*0.25;
bbs_out = bbs*0.1*0.25;
bri_out = bri*0.7*0.6/2;
Given the table, T, you defined, apply the function, func to the rows of the table, T.
T(4,:) = rowfun(@func, T(3,:), 'OutputVariableNames', T.Properties.VariableNames);
2 Comments
Jian Wei
on 24 Jul 2014
The sample code was to show you how to use the rowfun function to solve the problem in your example. You might need to modify the input arguments and the definition of the func function to suit your needs.
Also, given the table in your example, you can can obtain the same result using the varfun function as below.
f1 = @(x) x*0.2*0.25;
f2 = @(x) x*0.1*0.25;
f3 = @(x) x*0.7*0.6/2;
T1(1,1) = varfun(f1, T(3,:),'InputVariables','AIR');
T1(1,2) = varfun(f2, T(3,:),'InputVariables','BBS');
T1(1,3) = varfun(f3, T(3,:),'InputVariables','BRI');
T(4,:) = T1;
Note that you might need to modify the input arguments of varfun and the definitions of the anonymous functions according to your equations and table.
Jonas
on 24 Jul 2014
1 Comment
Rajesh Sinha
on 20 Aug 2014
Hello Jonas, Can you please share your solution to us. I am facing similar situation. I am passing a table to another function with 2 input variables and but when i am accessing the data of table inside called function using variabke names it gives error. when i display table it shows both data so i do not how to select first and second column inside the called function.
eg . p_ret = varfun(@f1,T,'InputVariables',{'Var1','var2'});
function[out_var] = f1(p_derive_data2) p_derive_data2 = p_derive_data2.var1;
above gives errors.
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!