How to get the maximum consecutive NaNs for each column

4 Ansichten (letzte 30 Tage)
Tiago Dias
Tiago Dias am 26 Apr. 2018
Kommentiert: ARCHANA MAJHI am 15 Mai 2020
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

Akzeptierte Antwort

Ahmet Cecen
Ahmet Cecen am 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 Kommentar
ARCHANA MAJHI
ARCHANA MAJHI am 15 Mai 2020
If I have time series data i.e having lat and Lon Then how can I use your code?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by