Need help creating a code
조회 수: 1 (최근 30일)
이전 댓글 표시
I have two matrices. Column 1 of Matrix A and Column 2 of Matrix B both correspond to time (seconds).
Matrix a= [34.2 2; 34.5 4; 34.7 1; 35.0 3; 35.2 2; 35.6 4; 35.7 1; 35.9 3]
Matrix b= [0.2 34.2; 0.1 34.3; 0.3 34.3; 0.1 34.4; 0.6 34.5;0.2 34.6; 0.1 34.7; 0.4 34.8; 0.8 34.9; 0.6 35.0; 0.3 35.1; 0.2 35.2; 0.4 35.3; 0.6 35.3; 0.1 35.4; 0.7 35.5;0.2 35.6; 0.2 35.7; 0.8 35.8; 0.7 35.9; 0.8 36.0]
I want to extract certain rows of Matrix B based on Matrix A. In general I want to extract only between the times where Matrix A column 2 have the values 2 and 4. Value 2 indicates the time I want to begin extracting from Matrix B and the value for indicates the last time I want to extract from Matrix B. In the end I want something like this.
Matrix c= [0.2 34.2; 0.1 34.3; 0.3 34.3; 0.1 34.4; 0.6 34.5;0.2 35.2; 0.4 35.3; 0.6 35.3; 0.1 35.4; 0.7 35.5;0.2 35.6] *Here we only extracted from 34.2s to 34.5s & 35.2s to 35.6s
Can someone help me create a code to do this. Any help would be much appreciated.
댓글 수: 0
채택된 답변
Adam
2015년 7월 30일
timeVals = [a( a(:,2) == 2 ), a( a(:,2) == 4 )];
idxAboveMinTime = bsxfun( @ge, b(:,2), timeVals(:,1)' );
idxBelowMaxTime = bsxfun( @le, b(:,2), timeVals(:,2)' );
validRowIdx = max( idxAboveMinTime & idxBelowMaxTime, [], 2 );
c = b( validRowIdx, : )
should do the job I think. Might not be the most efficient method, but it avoids for loops at least!
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!