matrix NaN change value

10 ビュー (過去 30 日間)
Firas
Firas 2014 年 6 月 22 日
回答済み: Andrei Bobrov 2014 年 6 月 22 日
Hello MatLab users A small problem , i dont know if MatLab had a fonction to do that , look please to the figure to understund more i have 2 big matrix if M1(i,j)=NaN , we do like the picher (1.9= mean(1.8,2)) , we change NaN with the value in theright if NaN is in the first colomn and the left one if it is in the end colomn thank you ----------------------------------- i try with this script but it does not work
Matrice_sim_reelle; %355*600
Matrice_sim_reelle_ajustee = isnan(Matrice_sim_reelle) ;
Matrice_sim_reelle_ajustee_2 = Matrice_sim_reelle ;
for i=1:1:length(Matrice_sim_reelle_ajustee(:,1))
for j=1:1:length(Matrice_sim_reelle_ajustee(1,:))
if Matrice_sim_reelle_ajustee(i,j)== 1
Matrice_sim_reelle_ajustee_2(i,j)=999999999
end
end
i
j
end
Matrice_sim_reelle_ajustee_2
  1 件のコメント
dpb
dpb 2014 年 6 月 22 日
Check out John D'Errico's File Exchange submittal first...

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2014 年 6 月 22 日
It's possible but you need to have special code near the edges because you aren't treating them the same as other locations. For example for the 1.9 you only considered the south and west elements, not the 4 adjacent or the 8 adjacent elements, whereas for the one in the left column, you only looked at the east element (in the middle you don't look at east, only south and west for some unexplained reason). It would be a lot easier if you would just replace nan's with the mean or median of whatever surrounds it. If you want to do that I can give you code. Or you can pick any pattern you want (S,W), (N,S,E,W), (East, west), whatever - it doesn't matter, all that matters is that it's the same for every element in the array. For example, this code replaces NANs with the average of a 1 by 3 window around the element:
m1 = [2,5.2,6,8,0; 10,0,0.1,0.2,0.2; nan,1,20,0,2.8; 1.8, nan,1,2,5; 2.54,2,1,2,nan]
% Find nans
m_nans = isnan(m1);
% Set to zero
m1(m_nans) = 0
% Sum the numbers in a sliding 1 by 3 window.
theSums = conv2(m1, [1,1,1], 'same')
% Count the number of non-nans.
theCounts = conv2(single(~m_nans), [1,1,1], 'same')
% Get the means
theMeans = theSums ./ theCounts;
% Replace only the nans
m1(m_nans) = theMeans(m_nans)

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 22 日
Use variant
[ii,jj] = ndgrid(1:size(M1,1),1:size(M1,2));
l = ~isnan(M1);
[i0,j0] = find(l);
F = scatteredInterpolant(i0,j0,M1(l));
M2 = F(ii,jj);

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by