Matlab Tricks

Notes.

Interpolate missing measurements

Z.B. x = Timestamps, y = Messwerte wo fehlende Werte als NaN gekennzeichnet sind. idx = Indizes der vorhandenen Wertepaare, y_neu = neuer y-Vektor mit interpolierten und extrapolierten Werten:

>> x = 1:1:10;
>> y = [ 1 2 3 NaN 5 6 7 8 9 NaN ] * 0.1

y =

    0.1000    0.2000    0.3000       NaN    0.5000    0.6000    0.7000    0.8000    0.9000       NaN

>> idx = find(not(isnan(y)));
>> y_neu = interp1(x(idx), y(idx), x, 'linear', 'extrap')

y_neu =

    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000

Error / Confidence Ellipse

downloadwrap
##
## Error / Confidence Ellipse Calculation
##

clear all;
close all;

## errors in x/y (lon/lat) in [m]etres
xy = [
      [ -0.240455140316807, -0.821869692314379 ],
      [ -0.371349301488509, -1.10749513758707 ],
      [ -0.391289981105339, -1.14555356563021 ],
      [ -0.385411647587039, -1.10888686855707 ],
      [ -0.346028865254643, -1.06092575993138 ],
      [ -0.307490839459224, -1.01574811313042 ],
      [ -0.287570695765111, -1.08785033983997 ],
      [ -0.264479580521884, -1.11895011290347 ],
      [ -0.253969354239174, -1.11600608731489 ],
      [ -0.27535025942211, -1.14555356563021 ],
      [ -0.355549787359105, -1.17183589105983 ],
      [ -0.363387618421842, -1.161933210362 ],
      [ -0.341370011900334, -1.09973366422067 ],
      [ -0.341370011900334, -1.09973366422067 ],
      [ -0.34674976977584, -1.10824461394849 ],
      [ -0.341217617139875, -1.04620563147006 ],
      [ -0.328270011386157, -0.995461060659456 ],
      [ -0.327598344647369, -0.979991455951988 ],
      [ -0.313882180054484, -0.953227439691966 ],
      [ -0.307408377495204, -0.927855154286666 ],
      [ -0.294287681280651, -0.889796726113909 ],
      [ -0.282011742265721, -0.854521760010779 ],
      [ -0.27697816590771, -0.820638524877654 ],
      [ -0.277746725397676, -0.796657970687257 ],
      [ 0.209908065558811, -0.413022442612198 ],
      [ 0.183687209766255, -0.378978643945793 ],
      [ 0.16572656423989, -0.353445794891931 ],
      [ 0.155181370560958, -0.339207357505906 ],
      [ 0.159120902235198, -0.322185458050255 ],
      [ 0.148575709191426, -0.30794702066423 ],
      [ 0.152688330663853, -0.303611264091318 ],
      [ 0.127485322480571, -0.279470146007462 ],
      [ 0.125200339079679, -0.273742658349262 ],
      [ 0.115672993657031, -0.269406901661069 ],
      [ 0.12020799437225, -0.263679413873254 ],
      [ 0.129700373073174, -0.250832707457234 ],
      [ 0.14105535719067, -0.245105219799035 ],
      [ 0.136734677924221, -0.219572370860456 ],
      [ 0.141096588178604, -0.201158740434799 ],
      [ 0.126861151619237, -0.189864328766962 ],
      [ 0.103770036376011, -0.158764555703463 ],
      [ 0.0963545845679157, -0.147470143906011 ],
      [ 0.0587787446066776, -0.119153832912139 ],
      [ 0.0455611563429319, -0.117762101812518 ],
      [ 0.031498810205064, -0.119153832912139 ],
      [ 0.0194721593487797, -0.140350925277806 ],
      [ 0.00625457104569671, -0.138959194307801 ],
      [ -0.0270005895979055, -0.136175732238175 ],
      [ -0.0464426929367829, -0.146078412936006 ],
      [ -0.0520717383357743, -0.168667236401294 ],
      [ -0.0578738747914247, -0.178569917099125 ],
      [ -0.0783338251162646, -0.178569917099125 ],
      [ -0.0694781464324046, -0.198375278494788 ],
      [ -0.0868845545408845, -0.228083320472999 ],
      [ -0.090824085540627, -0.245105219799035 ],
      [ -0.23009604344592, -0.416555380913633 ],
      [ -0.229251286920793, -0.413771918973622 ],
      [ -0.254917904025143, -0.434969011339289 ],
      [ -0.27681808118641, -0.426458061611464 ],
      [ -0.357736930168315, -0.396910583296148 ],
      [ -0.383576637694164, -0.405421532894358 ],
      [ -0.398061362412176, -0.408204994963984 ],
      [ -0.419961538938283, -0.399694045236159 ],
      [ -0.42140176513955, -0.391183095508333 ],
      [ -0.446182394742688, -0.365650246454471 ],
      [ -0.448640468565079, -0.347236616028815 ],
      [ -0.452365678167237, -0.33299817864279 ],
      [ -0.445545694725624, -0.33299817864279 ],
      [ -0.444950225723982, -0.344292590440241 ],
      [ -0.435672167785481, -0.362706220736283 ],
];

## extract columns from xy
y = xy(:,1); # x error values [m]
x = xy(:,2); # y error values [m]


## calculate ellipse parameters
corrXY = sum( x .* y )
avgX = mean(x)
avgY = mean(y)
stdX = std(x)
stdY = std(y)
qxx = stdX * stdX;
qyy = stdY * stdY;
qxy = (corrXY / length(x)) - (avgX * avgY);

tmp = (qxx - qyy) / 2
tmp1 = ((qxx + qyy) / 2) + sqrt( (tmp * tmp) + (qxy * qxy) )
tmp2 = ((qxx + qyy) / 2) - sqrt( (tmp * tmp) + (qxy * qxy) )

tmp1 = max(tmp1, 0);
tmp2 = max(tmp2, 0);

## the ellipse parameters:
ellA = sqrt(tmp1)
ellB = sqrt(tmp2)
ellO = atan2(2 * qxy, qxx - qyy) / 2 * 180 / pi


## plot
hold on;
figure(1);
plot(x, y', "k+;errors;", avgX, avgY, "r*;mean;");
grid on;
axis([-1.5, 0.5, -1, 1]);

## std error ellipse
drawEllipse([ avgX, avgY, 1.0 * ellA, 1.0 * ellB, ellO ])
## 95% error ellipse
drawEllipse([ avgX, avgY, 2.4477 * ellA, 2.4477 * ellB, ellO ])