How to count sequential NaN values

1 次查看(过去 30 天)
Steven Crisp
Steven Crisp 2015-12-9
编辑: dpb 2015-12-10
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 个)

dpb
dpb 2015-12-9
编辑:dpb 2015-12-10
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
>>
  1 个评论
Steven Crisp
Steven Crisp 2015-12-9
Yes kind of. I want the output for every sequence of nan's to be
col 1, 1 instance, row 6
col 2, 3 instance, row 1
col 2, 1 instance, row 5
col 3, 2 instance, row 3
Where I just want the sequential NaN's from columns, with no relation between columns. A new column is a whole new assortment.
Does that make sense?

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by