フィルターのクリア

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

2 ビュー (過去 30 日間)
SRB
SRB 2019 年 3 月 21 日
コメント済み: SRB 2019 年 3 月 21 日
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 Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by