remove first s and last t rows of a matrix containing NaN, leave lows in the middle containing NaN.
6 views (last 30 days)
Show older comments
I have a Tx2 Matrix A and I would like to remove the rows in the beginning and in the end that contain any NaN. For example:
A=[[NaN;NaN;NaN;4;1;NaN;5;6;8;NaN;NaN],[NaN;NaN;2;7;6;5;NaN;6;18;2;NaN]]
should the equal to:
A=[[4;1;NaN;5;6;8],[7;6;5;NaN;6;18]]
many thanks for your help, Jo.
0 Comments
Accepted Answer
Thorsten
on 4 Dec 2014
There might be smarter solutions to figure out the indices of leading and trailing 1's in nanflag, but this solution works:
A=[[NaN;NaN;NaN;4;1;NaN;5;6;8;NaN;NaN],[NaN;NaN;2;7;6;5;NaN;6;18;2;NaN]];
nanflag = isnan(sum(A'));
ind = []; for i = 1:numel(nanflag), if nanflag(i) == 1, ind = [ind i]; else break, end, end
for i = numel(nanflag):-1:1, if nanflag(i) == 1, ind = [ind i]; else break, end, end
Anew = A(setdiff(1:size(A,1), ind), :);
More Answers (2)
Guillaume
on 4 Dec 2014
A=[[NaN;NaN;NaN;4;1;NaN;5;6;8;NaN;NaN],[NaN;NaN;2;7;6;5;NaN;6;18;2;NaN]];
removestart = logical(sum(cumprod(isnan(A)), 2));
removeend = flipud(logical(sum(cumprod(flipud(isnan(A))), 2)));
A(removestart | removeend, :) = []
3 Comments
Guillaume
on 4 Dec 2014
Edited: Guillaume
on 4 Dec 2014
Works fine on R2014b, which version are you using?
In any case if cumprod does not accept logical, just convert them to double:
A=[[NaN;NaN;NaN;4;1;NaN;5;6;8;NaN;NaN],[NaN;NaN;2;7;6;5;NaN;6;18;2;NaN]];
nanpos = double(isnan(A));
removestart = logical(sum(cumprod(nanpos), 2));
removeend = logical(sum(cumprod(nanpos, 'reverse'), 2));
A(removestart | removeend, :) = []
I've also simplify the calculation of removeend. I didn't realise that cumprod had a reverse option.
Shame you accepted a less efficient code.
C.J. Harris
on 4 Dec 2014
Nobody should ever need more than one line:
A = [[NaN;NaN;NaN;4;1;NaN;5;6;8;NaN;NaN],[NaN;NaN;2;7;6;5;NaN;6;18;2;NaN]];
A2 = A(find(~any(isnan(A),2),1,'first'):find(~any(isnan(A),2),1,'last'),:);
0 Comments
See Also
Categories
Find more on Logical 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!