Total Pageviews

Blog Archive

Teaching (241) Mathematica (158) 3Q (50) Excel (45) SPSS (32) Cat (24) LaTeX (23) Mac (22) 君達呀 (15) Kaohsiung (14) 論文測試 (13) Mathematica教學講義 (3)

 

Followers

Mathematica 教學:對遺失資料做線性插值

Chung-Yuan Dye 於 Sunday, February 26, 2012 3:37 PM 發表


datatemp={
{-818.2106,-6755.208,207.9009},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{-767.4377,-6709.237,251.7202},
{-768.7812,-6700.438,253.0015},{-768.6588,-6701.238,252.8851},
{-767.7062,-6697.338,254.8504},{-765.7729,-6696.207,257.0257},
{-766.2335,-6689.349,259.0551},{-761.753,-6693.4,261.8711},
{-762.6193,-6685.736,264.3021},{-757.8909,-6690.449,267.0908},
{-754.3521,-6692.645,269.7541},{-752.003,-6692.321,272.2921},
{-750.8434,-6689.48,274.7048},{-750.8735,-6684.12,276.9922},
{-749.6801,-6681.277,279.5483},{-748.7109,-6677.929,282.1369},
{-747.9661,-6674.078,284.7577},{-747.4456,-6669.722,287.4109},
{-747.1495,-6664.861,290.0965},{-744.986,-6667.709,291.8721},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{-714.5345,-6624.671,368.8935}
}

mytrans[datatemp_List]:=Block[{data,zeroindex},

(*先將讀入資料加進流水號*)

data=Flatten[{#,datatemp[[#]]}]&/@Range[Length@datatemp];

(*列出斷點*)

zeroindex=Split[Select[data,#[[2]]==0&][[All,1]],#2-#1==1&];

(*斷點內線性插值*)

test[z_]:=Block[{incr},

(*找出斷點前後的資料*)

data[[#]]&/@{zeroindex[[z,1]]-1,zeroindex[[z,-1]]+1};

(*計算插值並替換資料*)

incr=MapThread[#2-#1&,data[[#]]&/@{zeroindex[[z,1]]-1,
zeroindex[[z,-1]]+1}];
Map[(data[[#]]=data[[#-1]]+incr/incr[[1]])&,zeroindex[[z]]]];

(*計算所有斷點插值*)

test[#]&/@Range[Length@zeroindex];
data
]


With[{dd=mytrans[datatemp]},
GraphicsGrid@
Transpose@{ListPlot[datatemp[[All,#]],PlotRange->All]&/@{1,2,3},
ListPlot[dd[[All,#]],PlotRange->All]&/@{2,3,4}}]

Tags:

讀者回應 ( 0 意見 )

Post a Comment

Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.

如果這篇文章對你有幫助,那請留個訊息給我~