フィルターのクリア

How can I select a part of an array between NaN values

2 ビュー (過去 30 日間)
Joanie
Joanie 2014 年 6 月 11 日
回答済み: Andrei Bobrov 2014 年 6 月 11 日
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).

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 11 日
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};

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by