Repeating table calculations using pairs of columns
1 次查看(过去 30 天)
显示 更早的评论
I have a table of data and need to do repeat calculations using two columns at a time. One of the columns is always the same (DOY), the other column changes across the spreadsheet. How can I do this without just doing them pair by pair. I am trying to predict a value based on a fitted straight line equation between 2 points. My code is: id1=find(isnan(x)); id2 =find(isnan(y)); x([id1,id2])=[] y([id1, id2])=[] p=polyfit(x,y,1) u=polyval(p,12)
An example of the data is attached. I am ne to MATLAB so if you have other better suggestions I'd be very happy to hear them!
0 个评论
回答(1 个)
Peter Perkins
2018-5-14
It's likely this can be solved using varfun. That takes a function handle and a table and applies the function to each variable in the table. You can tell it which variables in the table to work on (all but the DoY), and you can give it a function handle that uses your DoY variable. something like
t2 = varfun(@(x) myfun(x,t1.DoY), t1, 'InputVariables',2:end)
(assuming DoY is the first variable in t1.
3 个评论
Peter Perkins
2018-5-17
x is just "dummy" name for the variable that varfun is applying your function to. I hope this simple example will help:
>> t = table([1;2;3],[4;5;6],[1;-1;1],'VariableNames',{'X' 'Y' 'Sign'})
t =
3×3 table
X Y Sign
_ _ ____
1 4 1
2 5 -1
3 6 1
>> varfun(@(x) x.*t.Sign,t,'InputVariables',{'X' 'Y'})
ans =
3×2 table
Fun_X Fun_Y
_____ _____
1 4
-2 -5
3 6
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!