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

조회 수: 9 (최근 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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by