I need to check cell value along each diagonal. then if condition which I have define is satisfying, cells which have NaN value should replace by interpolation of cells beside NaN value cells.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
for q=1:nBinsY-2
if q==1
for p=3:nBinsX
s=p;
for i = 1:s-1
j = p;
if ~isnan(minz(i,j)) && isnan(minz(i+1,j-1))
for jj=2:p-1
if ~isnan(minz(i+jj,j-jj))
dz=minz(i,j)-minz(i+jj,j-jj);
dx=minx(i,j)-minx(i+jj,j-jj);
dy=miny(i,j)-miny(i+jj,j-jj);
ddi=sqrt(square(dx)+square(dy));
if abs(dz)<1 && abs(ddi)<5
sl=dz/ddi;
ddx=dx/(jj);
ddy=dy/(jj);
for r=1:jj-1
minz(i+r,j-r)=minz(i,j)+(sl*r);
minx(i+r,j-r)=minx(i,j)+(ddx*r);
miny(i+r,j-r)=miny(i,j)+(ddy*r);
end
else
continue
end
else
continue
end
i=i+jj;
j=j-jj;
end
else
continue
end
p=p-1;
end
end
else
p=nBinsX
for i = q:p+1
j=p;
if ~isnan(minz(i,j)) && isnan(minz(i+1,j-1))
for jj=2:p-1
if ~isnan(minz(i+jj,j-jj))
dz=minz(i,j)-minz(i+jj,j-jj);
dx=minx(i,j)-minx(i+jj,j-jj);
dy=miny(i,j)-miny(i+jj,j-jj);
ddi=sqrt(square(dx)+square(dy))
if abs(dz)<1 && abs(ddi)<5
sl=dz/ddi;
ddx=dx/(jj);
ddy=dy/(jj);
for r=1:jj-1
minz(i+p,j-p)=minz(i,j)+(sl*r);
minx(i+p,j-p)=minx(i,j)+(ddx*r);
miny(i+p,j-p)=miny(i,j)+(ddy*r);
end
else
continue
end
else
continue
end
i=i+jj;
j=j-jj;
end
else
continue
end
p = p-1;
end
end
end
0 commentaires
Réponses (1)
SAI SRUJAN
le 25 Sep 2024
Hi Saumya,
I understand that you are working on a problem where you need to interpolate the NaN values along the diagonals of matrices minx, miny and minz.
Please go through the following updated code sample with comments to resolve the issue,
% Loop through each diagonal starting from the top row
for q = 1:nBinsY-2
if q == 1
% Iterate over diagonals starting from the top row
for p = 3:nBinsX
% Traverse each diagonal
for i = 1:p-1
j = p - i + 1; % Calculate column index for the diagonal
% Check if the current position is non-NaN and the next diagonal position is NaN
if j > 1 && ~isnan(minz(i, j)) && isnan(minz(i+1, j-1))
% Look ahead to find the next non-NaN value along the diagonal
for jj = 2:p-1
if i+jj <= nBinsY && j-jj > 0 && ~isnan(minz(i+jj, j-jj))
% Calculate differences in z, x, and y
dz = minz(i, j) - minz(i+jj, j-jj);
dx = minx(i, j) - minx(i+jj, j-jj);
dy = miny(i, j) - miny(i+jj, j-jj);
ddi = sqrt(dx^2 + dy^2); % Calculate distance between points
% Check if differences are within thresholds
if abs(dz) < 1 && abs(ddi) < 5
sl = dz / ddi; % Calculate slope for z interpolation
ddx = dx / jj; % Calculate x increment
ddy = dy / jj; % Calculate y increment
% Fill in NaN values along the diagonal
for r = 1:jj-1
minz(i+r, j-r) = minz(i, j) + (sl * r);
minx(i+r, j-r) = minx(i, j) + (ddx * r);
miny(i+r, j-r) = miny(i, j) + (ddy * r);
end
end
break; % Exit loop after interpolation
end
end
end
end
end
else
% Iterate over diagonals starting from the left column
for p = nBinsX:-1:q+1
for i = q:p
j = p - (i - q); % Calculate column index for the diagonal
% Check if the current position is non-NaN and the next diagonal position is NaN
if j > 1 && ~isnan(minz(i, j)) && isnan(minz(i+1, j-1))
% Look ahead to find the next non-NaN value along the diagonal
for jj = 2:p-1
if i+jj <= nBinsY && j-jj > 0 && ~isnan(minz(i+jj, j-jj))
% Calculate differences in z, x, and y
dz = minz(i, j) - minz(i+jj, j-jj);
dx = minx(i, j) - minx(i+jj, j-jj);
dy = miny(i, j) - miny(i+jj, j-jj);
ddi = sqrt(dx^2 + dy^2); % Calculate distance between points
% Check if differences are within thresholds
if abs(dz) < 1 && abs(ddi) < 5
sl = dz / ddi; % Calculate slope for z interpolation
ddx = dx / jj; % Calculate x increment
ddy = dy / jj; % Calculate y increment
% Fill in NaN values along the diagonal
for r = 1:jj-1
minz(i+r, j-r) = minz(i, j) + (sl * r);
minx(i+r, j-r) = minx(i, j) + (ddx * r);
miny(i+r, j-r) = miny(i, j) + (ddy * r);
end
end
break; % Exit loop after interpolation
end
end
end
end
end
end
end
The changes involved organizing the interpolation process into two main loops: one starting from the top row and moving rightwards, and another starting from the left column and moving downwards. This ensures that all possible diagonals are covered. Within each diagonal, the code identifies sequences of NaN values and uses linear interpolation between the nearest non-NaN values.
I hope this helps!
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!