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.

Respuestas (1)

dpb
dpb el 9 de Dic. de 2015
Editada: dpb el 10 de Dic. de 2015
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 comentario

Steven Crisp
Steven Crisp el 9 de Dic. de 2015
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?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 9 de Dic. de 2015

Editada:

dpb
el 10 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by