Is there a way to get MATLAB to filter out specific rows in arrays?

36 views (last 30 days)
I’ve been looking for a method to filter out specific rows of data in an array. I’ve got a < 750 x 3 double> array containg car data that looks like this:
10000.5 3 120
10001.0 5 112
10001.5 1 117
10002.0 4 119
10002.5 3 122
10003.0 2 120
10003.5 5 114
10004.0 1 118
10004.5 4 120
Where column 1 is track time, column 2 is the car number, and column 3 is the speed of the car at that specific track time. I’m looking for a method to filter out rows of data based on the car number. For example, if the data in the rows associated with car numbers 3 and 4 are not needed, I’d have a resulting array that looks like this:
10001.0 5 112
10001.5 1 117
10003.0 2 120
10003.5 5 114
10004.0 1 118
Eventhough I haven’t found a specific MATLAB funtion that will do this type of filtering, I would think this can be done in MATLAB. Any ideas are appreciated. Thanks.

Accepted Answer

Kelly Kearney
Kelly Kearney on 26 Jun 2013
Take a look at the ismember function:
data = [...
10000.5 3 120
10001.0 5 112
10001.5 1 117
10002.0 4 119
10002.5 3 122
10003.0 2 120
10003.5 5 114
10004.0 1 118
10004.5 4 120]
new = data(~ismember(data(:,2),[3 4]),:)

More Answers (1)

Lokesh Ravindranathan
Lokesh Ravindranathan on 26 Jun 2013
The following code does filtering for your problem.
a = [10000.5 3 120;
10001.0 5 112;
10001.5 1 117;
10002.0 4 119;
10002.5 3 122;
10003.0 2 120;
10003.5 5 114;
10004.0 1 118;
10004.5 4 120];
list1 = a(:,1,:)
list2 = a(:,2,:)
list3 = a(:,3,:)
keyIndex = (list2~=3) &(list2~=4)
b = [list1(keyIndex) list2(keyIndex) list3(keyIndex)]
The matrix b returns the matrix of interest. There are other ways of implementing the filtering, but this is the simplest I could think of.

Products

Community Treasure Hunt

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

Start Hunting!