Select all Rows that Contain A Certain Number and Any NaN's That follow Them

2 visualizaciones (últimos 30 días)
I have a 94x4 matrix. A truncated example is below:
A =
434.390000000000 1 NaN NaN
434.700000000000 NaN NaN 42.0570000000000
434.760000000000 1 NaN NaN
438.720000000000 NaN NaN 46.6870000000000
441.640000000000 16 NaN NaN
441.720000000000 NaN NaN 46.3582500000000
447.250000000000 NaN NaN 39.7590000000000
453.590000000000 NaN NaN 39.3142500000000
556.390000000000 1 NaN NaN
558.880000000000 NaN NaN 43.4190000000000
562.400000000000 NaN NaN 39.4925000000000
566.060000000000 NaN NaN 38.6045000000000
568.430000000000 NaN NaN 39.5280000000000
573.810000000000 NaN NaN 37.6187500000000
2009.14000000000 1 NaN NaN
2010.26000000000 1 NaN NaN
2010.51000000000 1 NaN NaN
2011.14000000000 1 NaN NaN
2013.80000000000 NaN NaN 39.1890000000000
2019.39000000000 NaN NaN 36.4470000000000
2020.89000000000 16 NaN NaN
2021.51000000000 16 NaN NaN
2023.18000000000 NaN NaN 36.4445000000000
What I would like to do is select all the rows that contain a 1 in column 2 as well as all the rows that follow the 1's that contain NaN's. From the above example this is what I would want:
B =
434.390000000000 1 NaN NaN
434.700000000000 NaN NaN 42.0570000000000
434.760000000000 1 NaN NaN
438.720000000000 NaN NaN 46.6870000000000
556.390000000000 1 NaN NaN
558.880000000000 NaN NaN 43.4190000000000
562.400000000000 NaN NaN 39.4925000000000
566.060000000000 NaN NaN 38.6045000000000
568.430000000000 NaN NaN 39.5280000000000
573.810000000000 NaN NaN 37.6187500000000
2009.14000000000 1 NaN NaN
2010.26000000000 1 NaN NaN
2010.51000000000 1 NaN NaN
2011.14000000000 1 NaN NaN
2013.80000000000 NaN NaN 39.1890000000000
2019.39000000000 NaN NaN 36.4470000000000
Any help is much appreciated and I am happy to clarify anything. Thanks!

Respuesta aceptada

TADA
TADA el 27 de Abr. de 2019
I think in this case a simple loop will be better than a fancy vectorised solution.
valid = false(size(A, 1),1);
valid(1) = A(1,2) == 1;
for i = 2:size(A, 1)
curr = A(i,2);
if curr == 1 || (isnan(curr) && valid(i-1))
valid(i) = true;
end
end
B = A(valid,:);

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by