Deleting NaN from a large array
254 views (last 30 days)
Show older comments
I have an extremely large array, and I am trying to delete every single NaN as the trials do not all have the same amount of variables. Is there any way to simply read the entirety of A and justs delete NaN? I have been trying to use isnan and I keep getting a deletion of everthing or only NaN back.
0 Comments
Accepted Answer
DGM
on 6 Oct 2021
Edited: DGM
on 6 Oct 2021
Depends on what "extremely large" means. Let's start by assuming it's not too large for this to work. Furthermore, it depends on what "delete" means.
In this example, I assume a generic array and I assume "delete" means "replace with zero" for example.
% build example array with
s = [10000 10000];
A = zeros(s);
idx = randi([1 prod(s)],100,1);
A(idx) = NaN;
A(isnan(A)) = 0; % replace NaN with zero
sum(isnan(A),'all') % show that all the NaNs are gone
In this second example, I assume a vector and I assume "delete" means "remove this element and collapse the vector"
% build example array with
s = [1000000 1];
A = zeros(s);
idx = randi([1 prod(s)],100,1);
A(idx) = NaN;
A = A(~isnan(A)); % remove NaNs
sum(isnan(A),'all') % show that all the NaNs are gone
Note that element removal was only demonstrated for a vector. This is simply because you can't remove single elements from a 2 (or more) dimensional array and collapse the array accordingly. In other words, you can't have "holes" in an array. Only in the 1D case does removing a single element result in an unambiguous means to collapse the array.
If neither of these are close to what you need, or if your array is so large that these can't work, we'll work from there.
5 Comments
Image Analyst
on 7 Oct 2021
@Colton McGarraugh 6400 by 120 is far from large. It's just a small fraction of the size of a typical digital image. If it were 10k by 10k by 8 bytes, then we'd be approaching large.
But I question your original ask. Why do you think you need to "delete" nans in the first place? It might not be necessary depending on what you want to do. For example many functions like mean() have an 'omitnan' option. Plus maybe you could just replace the nan with the median of surrounding values, like I do in my attached salt and pepper noise removal demo. Like DGM said, you can't just remove them because then you'd have holes or "ragged" edges on the 2-D matrix, neither of which is allowed.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!