MATLAB: How to select a part of an array between NaN values

arraynan

For example I have an array like this:
T = [NaN NaN; 1 3; 2 4; 5 6; 8 7; NaN NaN; NaN NaN; 4 5; 6 7; NaN NaN; NaN NaN; 1 2; 2 4; 5 6; NaN NaN; NaN NaN; 1 3; 5 6; 8 8; NaN NaN; NaN NaN; 5 3; 6 7; NaN NaN];
now I like to have a code were the user can select a part of this array by typing the number of the part. Number 1 starts after the first NaN NaN and ends with the second NaN:
part1 = [1 3; 2 4; 5 6; 8 7];
number 2 starts after the third NaN NaN and ends for the fourth NaN NaN:
part2 = [4 5; 6 7];
enc. (in this example there are 5 numbers you can select).

Best Answer

l = ~isnan(T(:,1));
k = [l(1);diff(l)] == 1;
ii = cumsum(k);
z = (1:size(T,1))';
outc = accumarray(ii(l),z(l),[],@(x){T(x,:)});
N = 2; % the number of the part
out = outc{N};