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

3 次查看(过去 30 天)
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").

采纳的回答

Walter Roberson
Walter Roberson 2019-3-21
编辑:Walter Roberson 2019-3-21
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.

更多回答(2 个)

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

KSSV
KSSV 2019-3-21
编辑:KSSV 2019-3-21
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 个评论
madhan ravi
madhan ravi 2019-3-21
编辑:madhan ravi 2019-3-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by