Deleting NaN from a large array
조회 수: 174 (최근 30일)
이전 댓글 표시
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
채택된 답변
DGM
2021년 10월 6일
편집: DGM
2021년 10월 6일
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
Image Analyst
2021년 10월 7일
@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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!