How to get the maximum consecutive NaNs for each column

6 ビュー (過去 30 日間)
Tiago Dias
Tiago Dias 2018 年 4 月 26 日
コメント済み: ARCHANA MAJHI 2020 年 5 月 15 日
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

採用された回答

Ahmet Cecen
Ahmet Cecen 2018 年 4 月 26 日
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 件のコメント
ARCHANA MAJHI
ARCHANA MAJHI 2020 年 5 月 15 日
If I have time series data i.e having lat and Lon Then how can I use your code?

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by