Remove rows with consecutive numbers
5 次查看(过去 30 天)
显示 更早的评论
If I have a matrix such as:
A = [1 2 3 4 5
3 5 7 9 11
1 1 4 5 7
3 5 6 6 9
1 4 10 15 16];
I would like to remove all the rows from matrix A that contain more than 4 consecutive numbers. In matrix A we can see that first row contains 5 consecutive numbers [1 2 3 4 5], so this row should be removed from matrix A yielding:
A = [3 5 7 9 11
1 1 4 5 7
3 5 6 6 9
1 4 10 15 16];
0 个评论
采纳的回答
Luna
2019-1-3
I wrote down a function your A (matrix), k (number of consecutive numbers) and result is newA.
Call that function in your workspace with defining A and k.
function newA = filterArray(A,k)
pattern = ones(1,k-1);
idx = contains(cellstr(num2str((diff(A')' == 1))),num2str(pattern));
newA = A(~idx,:);
end
更多回答(2 个)
Bruno Luong
2019-1-3
编辑:Bruno Luong
2019-1-4
A = [1 2 3 4 5;
3 5 7 9 11;
1 1 4 5 7;
3 5 6 6 9;
1 4 10 15 16];
% Remove all rows of A contain at least n consecustive numbers
n = 3;
D = diff(A,1,2);
A(arrayfun(@(i) ~isempty(strfind(D(i,:), ones(1,n))),1:size(D,1)),:) = []
Result:
A =
3 5 7 9 11
1 1 4 5 7
3 5 6 6 9
1 4 10 15 16
1 个评论
Luna
2019-1-3
When A is like below:
A = [1 2 3 4 10
3 4 5 9 11
1 2 4 5 7
3 5 6 6 9
1 4 10 15 16];
result should be like:
A = [1 2 4 5 7
3 5 6 6 9
1 4 10 15 16];
So she asks for 3 or more consecutive numbers as I understand.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!