Selecting a Range of Rows in An Array Based off a Certain Value.

9 visualizaciones (últimos 30 días)
SRB
SRB el 21 de Mzo. de 2019
Comentada: SRB el 21 de Mzo. de 2019
I have a 1796990 x 4 matrix. Below is a truncated example:
0.0100000000000000 NaN NaN 68.0860000000000
0.0200000000000000 NaN NaN 68.0860000000000
0.0300000000000000 NaN NaN 68.0860000000000
0.0400000000000000 NaN NaN 68.0860000000000
0.0500000000000000 NaN NaN 68.0860000000000
0.0600000000000000 NaN NaN 68.0860000000000
0.0700000000000000 2 1 68.0860000000000
0.0800000000000000 NaN NaN 68.0860000000000
0.0900000000000000 NaN NaN 68.0860000000000
0.100000000000000 NaN NaN 68.0860000000000
0.110000000000000 NaN NaN 68.0860000000000
0.120000000000000 NaN NaN 68.0860000000000
0.130000000000000 NaN NaN 68.0860000000000
0.140000000000000 2 7 68.0860000000000
0.150000000000000 NaN NaN 68.0860000000000
0.160000000000000 NaN NaN 68.0860000000000
What I would like to do is use the "1" in column 3 as a reference point in order to select the 6,000 rows that precede the "1" in column 3 (excluding the row that contains the "1").

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Mzo. de 2019
Editada: Walter Roberson el 21 de Mzo. de 2019
idx = find(YourMatrix(:,3) == 1); %will be a column vector
Extracted = YourMatrix( bsxfun(@plus, (-6000:-1).', idx.'), :);
Extracted now is (something times 6000) by 4, where "something" is the number of matches (I do not assume that there is only one match.) The first set of 6000 is the rows for the first match, the second set of 6000 is for the second match, and so on. This could be reshaped and permuted to a multidimensional array, if you decide the order you want to store the match groups in. For example,
permute( reshape(Extracted.', size(YourMatrix,2), 6000, []), [2 1 3])
would have as many panes in the third dimension as there were matches.

Más respuestas (2)

madhan ravi
madhan ravi el 21 de Mzo. de 2019
a(1:find(a(:,3)==1,1,'first')-1,:) % a your matrix

KSSV
KSSV el 21 de Mzo. de 2019
Editada: KSSV el 21 de Mzo. de 2019
Let A be your 1796990 x 4 matrix.
% To pick rows which has 1 in column 3
B = A(A(:,3)==1,:) ;
% % To pick rows which precede 1 in column 3
idx = find(A(:,3)==1)-1 ;
B = A(idx,:)
  1 comentario
madhan ravi
madhan ravi el 21 de Mzo. de 2019
Editada: madhan ravi el 21 de Mzo. de 2019
What I would like to do is use the "1" in column 3 as a reference point in order to select the 6,000 rows that precede the "1" in column 3 (excluding the row that contains the "1").
Beware this answer gives only one row not range of rows.

Iniciar sesión para comentar.

Categorías

Más información sobre Multidimensional Arrays 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