How to get the maximum consecutive NaNs for each column

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by