Only returning NaNs when trying to do a double for loop

I have a matrix of size (361,361) named sivTotEASE.
At specific index i.e. sivTotEASE(m,n) I want to compute the mean (omiting NaNs) of the 8 closest neighbors of sivTotEASE(m,n) and replace the computed value at the location of sivTotEASE(m,n).
I try to do this in a double for loop (first loop for the index corresponding to the rows and second loop for the index corresponding to the columns) but I end up with a matrix containing only NaNs. I don't even have the original values of my matrix anymore... It is just NaNs everywhere...
When I try to compute it manually I do get a real value. For exampe, for index m = 1000, n = 1000 I get the value 8.0114 and not NaN.
Here is my code:
%Load data (see attached files for data)
load('SICnoSIVrow.mat');
load('SICnoSIVcol.mat');
load('sivTotEASE.mat');
%Mean of the 8 closest neighbors for sivTotEASE(m,n)
sivTotNeighbor = sivTotEASE;
for m = SICnoSIVrow
for n = SICnoSIVcol
NeighborIt = nanmean(sivTotEASE(m-1:m+1,n-1:n+1),'all');
sivTotNeighbor(m,n) = NeighborIt;
end
end
%Manual test (the following line gives 8.0114)
%test = nanmean(sivTotEASE(m(1000)-1:m(1000)+1,n(1000)-1:n(1000)+1),'all')
Can anyone help me figure out what is the issue?
Thank you
****ACCEPTED ANSWER (see comments)****
%Edited code:
%Load data (see attached files for data)
load('SICnoSIVrow.mat');
load('SICnoSIVcol.mat');
load('sivTotEASE.mat');
%Compute neighbor's average
sivTotNeighbor = sivTotEASE;
for ii = 1:length(SICnoSIVrow)
for jj = 1:length(SICnoSIVcol)
m=SICnoSIVrow(ii);
n=SICnoSIVcol(jj);
NeighborIt = mean(sivTotEASE(m-1:m+1,n-1:n+1),'all','omitnan');
sivTotNeighbor(m,n) = NeighborIt;
end
end

 Accepted Answer

use this
mean(sivTotEASE(m-1:m+1,n-1:n+1),"omitNan")

11 Comments

Thank you Amit Bhowmick for your quick answer.
I tried the following:
sivTotNeighbor = sivTotEASE;
for m = SICnoSIVrow
for n = SICnoSIVcol
NeighborIt = mean(sivTotEASE(m-1:m+1,n-1:n+1),'all','omitnan');
sivTotNeighbor(m,n) = NeighborIt;
end
end
But it still only returns NaN. Do you know how to fix this?
Thank you
As per the attached file this variable is missing ('SICnoSIVcol'). So I can not check or verify.
This is my mistake, I edited my question with the proper variables.
All the values of variable load('sivTotEASE.mat') contains NaN.
There are indeed NaNs in the first rows and columns but it is not all NaNs
notnan = find(~isnan(sivTotEASE));
%notnansize = size(notnan) %returns 18115
There are in fact 18115 entries with non nan values
And if you use the following command without the NaN flag (as in your picture) you will indeed get a NaN instead of the value 8.0114 because the value is surrounded by some NaNs.
test = mean(sivTotEASE(m(1000)-1:m(1000)+1,n(1000)-1:n(1000)+1),'all','omitnan')
within the loop what is m and n ?
Are they scaler or vector
I think the following correction required
for ii = 1:length(SICnoSIVrow)
for jj = 1:length(SICnoSIVrow)
m=SICnoSIVrow(ii);
n=SICnoSIVrow(jj);
It is the index of rows (m) and columns (n) where I want to compute de average of the neighbors.
m is the vector from the variable SICnoSIVrow.mat and n is the vector from the variable SICnoSIVcol.mat
m = SICnoSIVrow;
n = SICnoSIVcol;
I think I solved my issue by writing the following:
%Load data (see attached files for data)
load('SICnoSIVrow.mat');
load('SICnoSIVcol.mat');
load('sivTotEASE.mat');
%Compute neighbor's average
sivTotNeighbor = sivTotEASE;
for m = [1:length(SICnoSIVrow)] %row
for n = [1:length(SICnoSIVcol)] %col
NeighborIt = mean(sivTotEASE(SICnoSIVrow(m)-1:SICnoSIVrow(m)+1,SICnoSIVcol(n)-1:SICnoSIVcol(n)+1),'all','omitnan');
%disp(NeighborIt);
sivTotNeighbor(SICnoSIVrow(m),SICnoSIVcol(n)) = NeighborIt;
end
end
%test = mean(sivTotEASE(SICnoSIVrow(1000)-1:SICnoSIVrow(1000)+1,SICnoSIVcol(1000)-1:SICnoSIVcol(1000)+1),'all','omitnan');
I missed your comment but I indeed think you are right with your suggestion to edit my code but jj and n would have to be for the variable SICnoSIVcol (and not for SICnoSIVrow):
for ii = 1:length(SICnoSIVrow)
for jj = 1:length(SICnoSIVcol)
m=SICnoSIVrow(ii);
n=SICnoSIVcol(jj);
This is also what I ended with (see comment above)!
Thank you

Sign in to comment.

More Answers (0)

Asked:

on 5 Jul 2021

Edited:

on 5 Jul 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!