12 July 2011

Behold, Some Code!

Since my life reboot, my leet code skillz have gone to whole new levels. Here's a representation of the kinds of things I've been doing for the past three months:

s = 1; %blort-cell counter
interpnum = cell(1,6);
for t = {East_u, North_v, Vert_w, VelError, Mag, DIR}
    blort = t{1};
    k = 1; %within blort-cell counter
    for i = 1:length(SerYear)
        badeast = find(isnan(blort(i,:)));
        for j = 1:length(badeast)
            m = badeast(j);
            while isnan(blort(i, m))
                if m > 1
                    m = m - 1;
                else
                    break
                end
            end
            n = badeast(j);
            while isnan(blort(i, n))
                if n < SerBins(end)
                    n = n + 1;
                else
                    break
                end
            end
            interpnum{s}(k, 1) = i;
            interpnum{s}(k, 2) = badeast(j);
            if n - m < 5 && ~isnan(blort(i, m)) && ~isnan(blort(i, n))
                interpnum{s}(k, 3) = interp1([m n], [blort(i, m) blort(i, n)], badeast(j));
            elseif n - m < 5 && isnan(blort(i, m)) && ~isnan(blort(i, n))
                interpnum{s}(k, 3) = blort(i, n);
            elseif n - m < 5 && ~isnan(blort(i, m)) && isnan(blort(i, n))
                interpnum{s}(k, 3) = blort(i, m);
            else
                interpnum{s}(k, 3) = NaN;
            end
            k = k + 1;
        end
        clear badeast j m n
    end
    clear i blort k
    s = s+1;
end
badeast = interpnum{1};
badnorth = interpnum{2};
badvert = interpnum{3};
baderr = interpnum{4};
badmag = interpnum{5};
baddir = interpnum{6};
clear s t interpnum

It looks at a big block of numbers, one row at a time, and interpolates for any missing values - assuming that there are no more than four missing in a row (in the row). It does this for six individual same-sized blocks.

It's in Matlab. There might actually be a simpler way to do this, but if so I don't know about it. (Obviously.) The fun part is: it only took me two days to work all that out. Leet skillz progress! :D