Need help creating a code

1 次查看(过去 30 天)
Sha S
Sha S 2015-7-30
回答: Adam 2015-7-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.

采纳的回答

Adam
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 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by