Repeating table calculations using pairs of columns

조회 수: 3 (최근 30일)
Wendy Cameron
Wendy Cameron 2018년 5월 9일
댓글: Wendy Cameron 2018년 5월 18일
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!

답변 (1개)

Peter Perkins
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
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
Wendy Cameron
Wendy Cameron 2018년 5월 18일
Thank you very much, that has it explained it very clearly. I can imagine a lot of applications for this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by