필터 지우기
필터 지우기

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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by