Vectorization of non-recursive equation made difficult by NaN
Show older comments
I am performing some simple manipulations in a 2-dimensional grid where certain nodes should not be taken into account. These are specified in a similar sized matrix with NaN on to be excluded nodes.
The calculations were originally performed in a double loop. A very simplified version would look like this:
new = zeros(5000,2000);
for j=2:size(new,2)-1
for i=2:size(new,1)-1
if(~isnan(nanmat(i,j)))
if(isnan(nanmat(i-1,j))), old(i-1,j)=old(i,j); end
if(isnan(nanmat(i+1,j))), old(i+1,j)=old(i,j); end
if(isnan(nanmat(i,j-1))), old(i,j-1)=old(i,j); end
if(isnan(nanmat(i,j+1))), old(i,j+1)=old(i,j); end
new(i,j) = old(i,j) + (old(i+1,j) + old(i-1,j) + old(i,j+1) + old(i,j-1))/4;
end
end
end
Not really elegant...
So I thought it wouldn't be too hard to eliminate these loops, which is true for most of the code...:
new = zeros(5000,2000);
is = 2:size(new,1)-1;
js = 2:size(new,2)-1;
notNaN = ~isnan(nanmat);
new(is,js) = old(is,js) + notNaN*(old(is+1,js) + old(is-1,js) + old(is,js+1) + old(is,js-1))/4;
the multiplication with notNaN compensates for the ~isnan check, but I cannot really seem to find a solution to account for the if-statements that check neighboring nodes. Is there an easy way to do so? These calculations are repeated very often and vectorizing this part of my code saves me about 15-20% in calculation time.
Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Nearest Neighbors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!