Remove rows with consecutive numbers
古いコメントを表示
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];
採用された回答
その他の回答 (2 件)
Luna
2019 年 1 月 3 日
Hello Sena,
Try this below:
newA = A(~all((diff(A')' == 1)')',:);
5 件のコメント
sena
2019 年 1 月 3 日
Luna
2019 年 1 月 3 日
Ups sorry it was my mistake i didn't read the question well.
Here it checks the sum of the colums in diff of A which are greater than 3 gets deleted.
newA = A(~(sum((diff(A')' == 1)') >= 3)',:)
Luna
2019 年 1 月 3 日
Actually I realized that, in the question you said it will be more than 4 consecutive numbers that means 5 consecutive numbers. First code I shared does the exact thing you said. That means all row:
Now you say 3 or more numbers.
sena
2019 年 1 月 3 日
Luna
2019 年 1 月 3 日
Please look at the function I have given in another answer.
Write your k -> any number of consecutive you wish and your A matrix.
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.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!