Data filtering(Give a constraint to the length of each index )
2 次查看(过去 30 天)
显示 更早的评论
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
2019-8-19
In other words: You want to keep only the first n-1 rows for each value in the first column.
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!