How to get the maximum consecutive NaNs for each column

3 visualizaciones (últimos 30 días)
Tiago Dias
Tiago Dias el 26 de Abr. de 2018
Comentada: ARCHANA MAJHI el 15 de Mayo de 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

Respuesta aceptada

Ahmet Cecen
Ahmet Cecen el 26 de Abr. de 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 comentario
ARCHANA MAJHI
ARCHANA MAJHI el 15 de Mayo de 2020
If I have time series data i.e having lat and Lon Then how can I use your code?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by