Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
SRB
SRB am 21 Mär. 2019
Kommentiert: SRB am 21 Mär. 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").

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Mär. 2019
Bearbeitet: Walter Roberson am 21 Mär. 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.

Weitere Antworten (2)

madhan ravi
madhan ravi am 21 Mär. 2019
a(1:find(a(:,3)==1,1,'first')-1,:) % a your matrix

KSSV
KSSV am 21 Mär. 2019
Bearbeitet: KSSV am 21 Mär. 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 Kommentar
madhan ravi
madhan ravi am 21 Mär. 2019
Bearbeitet: madhan ravi am 21 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by