From: Matt Fig on
Perhaps I misunderstand, but if the nans are only in the second page wouldn't it be much faster to just do:


[r,c] = find(isnan(A(:,:,2)));
for ii = 1:length(r)
A(r(ii),c(ii),2) = (A(r(ii),c(ii),1)+A(r(ii),c(ii),3))/2;
end
From: Matt Fig on
Even if there are nans in the other dimensions, I would think this would still be faster:


IDX = 1:3;
B = A; % To preserve the original. Not necessary.
for jj = 1:3
S = IDX(~(IDX==jj));
[r,c] = find(isnan(B(:,:,jj)));

for ii = 1:length(r)
B(r(ii),c(ii),jj) = (B(r(ii),c(ii),S(1))+B(r(ii),c(ii),S(2)))/2;
end
end