필터 지우기
필터 지우기

Calculations in table based on variable names

조회 수: 3 (최근 30일)
Jonas
Jonas 2014년 7월 21일
댓글: Rajesh Sinha 2014년 8월 20일
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.

답변 (2개)

Jian Wei
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
Jonas
Jonas 2014년 7월 22일
Thank you for the answer, unfortunately, it does not work exactly the way I need it. While it calculates the fourth row correctly in this specific case, it does not work in general. What I need is to define the correct coefficients (equations) for all the 100+ variables and then just use it for the various selected sub-tables. If I use your definition of the function and increase the number of defined variables that are not at the same time contained in the selected sub-table, I get the following error:
Error using table/rowfun>dfltErrHandler (line 311)
Applying the function 'func' to the 1st row of A generated the following error:
Not enough input arguments.
Error in table/rowfun>@(s,varargin)dfltErrHandler(grouped,funName,s,varargin{:}) (line 195)
errHandler = @(s,varargin) dfltErrHandler(grouped,funName,s,varargin{:});
Error in table/rowfun (line 218)
[b_data{i,:}] = errHandler(struct('identifier',ME.identifier, 'message',ME.message,
'index',i),inArgs{:});
Any suggestions?
Jian Wei
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
Jonas 2014년 7월 24일
In the end, the solution turned out to be quite different but simple. I just stored the coefficients in a separate table and used intersect to find the correct values to use in the calculations.
  댓글 수: 1
Rajesh Sinha
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by