Calculations in table based on variable names
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Jian Wei
2014년 7월 21일
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
Jian Wei
2014년 7월 24일
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
2014년 7월 24일
댓글 수: 1
Rajesh Sinha
2014년 8월 20일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!