Data filtering(Give a constraint to the length of each index )

1 回表示 (過去 30 日間)
Jaehwi Bong
Jaehwi Bong 2019 年 8 月 19 日
編集済み: Jan 2019 年 8 月 19 日
I have this kind of data. First column and second column refer to an index and x value relatively.
data = [ 1 201; 1 202; 2 301; 2 313; 2 311; 3 401; 3 452; 3 433; 3 405; 4 504; 4 303; 4 604; 4 703; 5 600; 5 700; 5 606; 5 703; 5 905; 5 444;];
For example, I want to delete from after 4th data in each index if the number of index is over 4.
The length of 3rd, 4th and 5th index is 4, 4, 6 relatively in this example data. I'd like to keep their data only from 1st to 3rd values.
Every index has less number than 4.
data_filtered = [1 201; 1 202; 2 301; 2 313; 2 311; 3 401; 3 452; 3 433; 4 504; 4 303; 4 604; 5 600; 5 700; 5 606;];
If anyone can help, it would be greatly appreciated.
Thank you!
  1 件のコメント
Jan
Jan 2019 年 8 月 19 日
In other words: You want to keep only the first n-1 rows for each value in the first column.

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

採用された回答

Jan
Jan 2019 年 8 月 19 日
編集済み: Jan 2019 年 8 月 19 日
There are more efficient ways, but starting with a simple loop is a good apporach:
data = [ 1 201; 1 202; 2 301; 2 313; 2 311; 3 401; 3 452; 3 433; ...
3 405; 4 504; 4 303; 4 604; 4 703; 5 600; 5 700; 5 606; 5 703; ...
5 905; 5 444];
nRow = size(data, 1);
keep = false(1, nRow);
index = -1; % any not occurring index
n = 4; % keep n-1 indices
for k = 1:nRow
if data(k, 1) ~= index
index = data(k, 1);
count = 0;
end
count = count + 1;
keep(k) = count < n;
end
data_filtered = data(keep, :)
A vectorized method:
index = data(:, 1);
D = [false ; diff(index(:)) ~= 0] ;
Start = [1; find(D)]; % Start indices of next run
N = ones(size(D));
N(D) = 1 - diff(Start);
N = cumsum(N);
data_filtered = data(N < 4, :)
See also Jos' FileExchange submission https://www.mathworks.com/matlabcentral/fileexchange/56131-runindex . Then:
N = runindex(data(:, 1));
data_filtered = data(N < 4, :)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by