How can I synchronize nan across dimensions of a matrix?

5 views (last 30 days)
How can I set all elements that are nan in one dimension also nan in another dimension?
My example: I have a 3d-matrix A of size(A)
ans = 34168 30 13
Let this any model output for e.g. 34168 grid points g in a map, 30 years y and 13 models m. Now I need to set all values of a given model m to nan, if the value from any other model m at that position is nan. In other words: I need to set A(g,y,1) nan if A(g,y,2) is nan, A(g,y,1) nan if A(g,y,3) is nan...and that for all g,y, and m.
Now I have a loop which should do the job, but it seems that 16 GB RAM are not enough, since it takes forever:
A=rand(34168,30,13); %just for illustration
b=randi(numel(A),1000000,1);
A(b)=nan; %set random numbers to nan
m=1;
for m=1:size(A,3)
[row{m} col{m}]=find(isnan(A(:,:,m)));
end
tic
m=1;
for m=m:size(A,3)
n=1;
for n=n:size(A,3)
disp(num2str([m n]))
A(row{n},col{n},m)=nan;
end
end
toc
%After synchronization the number of nan in each m should be the same, e.g. test through
length(find(isnan(A(:,:,1))))==length(find(isnan(A(:,:,2))))
I know, these are a lot of nan (>10 % of elements), but there should be a way to make this way faster.
Any help and hint is greatly appreciated.
Cheers,
yolgs

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!