Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

neil jerome
neil jerome el 28 de Oct. de 2020
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 comentarios
Vasilis Margaritis
Vasilis Margaritis el 31 de Oct. de 2020
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 el 1 de Nov. de 2020
no problem, glad it helped! grateful if you could 'accept' the answer :) cheers, n.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by