How to count sequential NaN values
古いコメントを表示
Hello,
I have a matrix
x = [1,NaN,3;4,NaN,6;7,NaN,NaN;1,2,NaN;3,NaN,3;NaN,9,1]
creates
1 NaN 3
4 NaN 6
7 NaN NaN
1 2 NaN
3 NaN 3
NaN 9 1
I want to be able to write to a variable every time a series of NaN (1 or more) occur in a column, and how many.
For example for the above code I want it to calculate
1 occurance for column 1 for 1 instance
1 occurance for column 2 for 3 instances
1 occurance for column 2 for 1 instance
1 occurance for column 3 for 2 instances
Do I put in a for loop for each column, search for NaN, then use "Diff" to find a difference of zero, then record how many gaps there are?
Or, do I delete every NaN value, then search for gaps in the data and use "Diff" like below?
threshold = 5;
gap = diff(data1);
idx = find(gap>threshold);
Thank you.
回答 (1 件)
Presuming I get your intent,
>> sum(diff([zeros(1,3);isnan(x)])==1)
ans =
1 2 1
>>
ADDENDUM
Locations...
>> [i j]=find(diff([zeros(1,3);isnan(x)])==1);
>> [i j]
ans =
6 1
1 2
5 2
3 3
>>
カテゴリ
ヘルプ センター および File Exchange で NaNs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!