Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Joanie
Joanie am 11 Jun. 2014
Beantwortet: Andrei Bobrov am 11 Jun. 2014
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).

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 11 Jun. 2014
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};

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by