필터 지우기
필터 지우기

Overwrite 'NaN' cells of an array with non NaN cells of another

조회 수: 1 (최근 30일)
Vasilis Margaritis
Vasilis Margaritis 2020년 10월 27일
댓글: neil jerome 2020년 11월 1일
Hello,
i have a 3D array final_product(360,180,8) of satellite data over earth at 8 different wavelenghts. I also have a second 3D array B(360,180,3). There are a lot of empty cells (NaN). If a specific cell in array final_product is 'NaN' and the cell with same coordinates in array B is not 'NaN', i want the later to replace the cell in the array final_product, else, leave the original cell intact.
My progress is:
for i=1:1
filename=files(i).name;
A=ncread(filename,'Aerosol_Optical_Depth_Average_Ocean_QA_Mean');
final_product(:,:,2:8)=A;
B=ncread(filename,'Aerosol_Optical_Depth_Land_QA_Mean');
if final_product(isnan(final_product));
final_product(:,:,2:4)=B;
end
end
Ignore the "for" loop..
I get an error "NaN's cannot be converted to logicals"
If anyone can halp, Thanks in advance!

채택된 답변

neil jerome
neil jerome 2020년 10월 28일
hi vasilis,
have a look through this: rather than using a loop (which should work fine, but probably slower) you can use indexing. comments below hopefully helpful; since you describe matrices of different sizes, there's an extra step to make them equal. there's lots of equivalent ways of working around this, depending how many assumptions you want to make (ie are the data always the same size, etc).
%% this part shows the logic
% simple 1D or 2D data (equal size)
FinalProduct = NaN(1,6); FinalProduct(4) = 45; B = NaN(1,6); B(3) = 12;
% FinalProduct = NaN(5); FinalProduct(find(eye(5))) = 123; B = fliplr(FinalProduct)/2;
% these are the indices you want to replace
targetIndices = find(isnan(FinalProduct) .* ~isnan(B));
%replace these indices in a using values from b
NewResult = FinalProduct; % just to illustrate
NewResult(targetIndices) = B(targetIndices);
% display output for sanity check
FinalProduct
B
NewResult
%% for the data you described:
% for 3D data of unequal dimensions, 'easiest' (?) to make dummy for B
% this allows the earlier logic to work exactly the same
% strictly, this assumes FinalProduct > B, otherwise need to 'fill out' smaller to fit larger
%% Read in your data here
% FinalProduct =
% B =
sizeOfFinalProd = size(FinalProduct);
sizeOfB = size(B);
dummyB = NaN(sizeOfFinalProd);
dummyB(1:sizeOfB(1),1:sizeOfB(2),1:sizeOfB(3)) = B; % or as many dimensions as needed :)
% now FinalProduct and dummyB are the same size, with B padded out with NaN
targetIndices = find(isnan(FinalProduct) .* ~isnan(dummyB));
NewResult = FinalProduct;
NewResult(targetIndices) = dummyB(targetIndices);
  댓글 수: 2
Vasilis Margaritis
Vasilis Margaritis 2020년 10월 31일
Thank you! The way you presented helped me a lot. The loop is there for another reason, to do the same thing again for data of other days. Maybe i souldn't have written it at all. Sorry, also, for not using the code syntax! thanks again!
neil jerome
neil jerome 2020년 11월 1일
no problem, glad it helped! grateful if you could 'accept' the answer :) cheers, n.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by