Here's one approach, which uses functions to encapsulate parts of the task. By creating a specialized version of the median function that ignores NaN
, it's easier to use mapslices
:
function findcors(oned, windows)
samplesPerWindow = size(oned, 1) / windows
cors = zeros(size(oned, 2), size(oned, 2), windows)
for i = 1:windows
startat = (i - 1) * samplesPerWindow + 1
endat = i * samplesPerWindow
corofsamples = cor(oned[startat:endat, :])
cors[:, :, i] = corofsamples
end
return cors
end
function nanmedian(A)
cleanA = A[isfinite(A)]
if isempty(cleanA)
NaN
else
return median(cleanA)
end
end
oned = rand(200, 264)
oned[:, 3] = NaN
oned[:, 200] = NaN
cors = findcors(oned, 10)
med = mapslices(nanmedian, cors, 3)
I believe your original code was using the wrong window length inside the main loop. Hopefully I've fixed that.
The DataFrames package provides an NA
value and tools to ignore NA
, but still needs to clean up its median
function to exploit those tools.