MATLAB: Remove first s and last t rows of a matrix containing NaN, leave lows in the middle containing NaN.

remove nan

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.

Best Answer

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), :);