Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Joanie
Joanie el 11 de Jun. de 2014
Respondida: Andrei Bobrov el 11 de Jun. de 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).

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 11 de Jun. de 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};

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by