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

19 ビュー (過去 30 日間)
Brad
Brad 2013 年 6 月 26 日
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.

採用された回答

Kelly Kearney
Kelly Kearney 2013 年 6 月 26 日
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]),:)
  1 件のコメント
Brad
Brad 2013 年 6 月 26 日
Kelly, good catch. I forgot all about this function. Thanks.

サインインしてコメントする。

その他の回答 (1 件)

Lokesh Ravindranathan
Lokesh Ravindranathan 2013 年 6 月 26 日
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by