Clear Filters
Clear Filters

How to get the maximum consecutive NaNs for each column

2 views (last 30 days)
Hello,
So i got a matrix A with 2 columns and 10 rows for example, with numbers and NaN's:
A =
1 2
3 4
NaN NaN
NaN 7
8 9
NaN NaN
0 NaN
9 NaN
NaN NaN
NaN 8
I want to calculate for each column of A the maximum number of consecutive NaN's, so the result would be like
result = [2 4]
I saw this code for a vector, but in my case since i got multiple columns doesnt quite do what i want
nanLocations = isnan(A) % Get logical array of whether element is NaN or not.
props = regionprops(nanLocations, 'Area', 'PixelIdxList'); % Find all the regions.
% DONE! Now let's print them out
for k = 1 : length(props)
fprintf('Region #%d has length %d and starts at element %d and ends at element %d\n',...
k, props(k).Area, props(k).PixelIdxList(1), props(k).PixelIdxList(end));
end

Accepted Answer

Ahmet Cecen
Ahmet Cecen on 26 Apr 2018
Here you go:
result = zeros(1,size(A,2));
for i = 1:size(A,2)
B = isnan(A(:,i));
CC = bwconncomp(B);
Sizes = cellfun(@length, CC.PixelIdxList);
result(i) = max(Sizes);
end
  1 Comment
ARCHANA MAJHI
ARCHANA MAJHI on 15 May 2020
If I have time series data i.e having lat and Lon Then how can I use your code?

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!