sliding window median of correlations of a matrix -
question parts:
- is there "julia way" implement sliding window?
- what needed in julia ignore
nan
s?
there matrix 264 recording points (rows) , 200 time points (columns). want median correlation of each recording point every other point on 10 sample window.
i've tried matlab-way (tm) creating 3d 264x264x10 matrix third dim correlation window. in matlab, median(cors,3)
julia can mean(cors,3)
. median not have support this. looks mapslices(median,cors,3)
might want, recording points have nans. in r, might na.omit()
or function options na.ignore=t
don't see julia.
#oned=readdlm("10152_20111123_preproc_torque.1d") oned=rand(200,264); oned[:,3]=nan; oned[:,200]=nan windows=10 samplesperwindow=size(oned,1)/windows cors=zeros(size(oned,2),size(oned,2),windows) i=1:windows startat=(i-1)*windows+1 endat=i*windows corofsamples=cor(oned[startat:i*windows,:]) cors[:,:,i]= corofsamples end med = mapslices(median,cors,3) # fail b/c nan
here's 1 approach, uses functions encapsulate parts of task. creating specialized version of median function ignores nan
, it's easier use mapslices
:
function findcors(oned, windows) samplesperwindow = size(oned, 1) / windows cors = zeros(size(oned, 2), size(oned, 2), windows) = 1:windows startat = (i - 1) * samplesperwindow + 1 endat = * 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 original code using wrong window length inside main loop. i've fixed that.
the dataframes package provides na
value , tools ignore na
, still needs clean median
function exploit tools.
Comments
Post a Comment