Need help creating a code

4 vues (au cours des 30 derniers jours)
Sha S
Sha S le 30 Juil 2015
Réponse apportée : Adam le 30 Juil 2015
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.

Réponse acceptée

Adam
Adam le 30 Juil 2015
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!

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by