Find the begining and ending on nan sequence and store
1 view (last 30 days)
Show older comments
Hi,
A = [7 9;NaN 9; NaN NaN; 4 NaN; 6 7;7 9;NaN 9; NaN NaN; 4 NaN; 6 7]
In both columns, i got 2 sequences of NaN's of length 2.
For each column i want to store the begining index and ending index of each sequence, and the length of it.
To something like:
result = cell(#number of NaN sequences of A), Size(A,2))
result = (2,3,2) (3,4,2)
(7,8,2) (8,9,2)
And "result" could be a cell, where the column of "result" is all calculations related to column 1 of A.
hope it was clear enough,
Thanks
TLDR: I can only find the index of NaN values in my matrix A, i need a way to find the begining and end of each sequence and their length per column.
0 Comments
Accepted Answer
Stephen23
on 24 Sep 2018
Edited: Stephen23
on 24 Sep 2018
>> A = [7,9;NaN,9;NaN,NaN;4,NaN;6,7;7,9;NaN,9;NaN,NaN;4,NaN;6,7]
A =
7 9
NaN 9
NaN NaN
4 NaN
6 7
7 9
NaN 9
NaN NaN
4 NaN
6 7
>> T = false(1,size(A,2));
>> D = diff([T;isnan(A);T],1,1);
>> [Rb,Cb] = find(D>0);
>> [Re,Ce] = find(D<0);
>> [Rb,Re-1,Re-Rb,Cb] % [start,end,length,column]
ans =
2 3 2 1
7 8 2 1
3 4 2 2
8 9 2 2
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!