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];

 Réponse acceptée

Luna
Luna le 3 Jan 2019
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

3 commentaires

Luna
Luna le 3 Jan 2019
Modifié(e) : Luna le 3 Jan 2019
for example:
k = 3;
A = [..];
pattern = ones(1,k-1);
idx = contains(cellstr(num2str((diff(A')' == 1))),num2str(pattern));
newA = A(~idx,:);
%% OR
myNewResult = filterArray(A,3);
sena
sena le 3 Jan 2019
It works perfectly! I just checked it with my large matrix and it looks all good.
Thank you, Luna, very much. You have been a great help.
Luna
Luna le 3 Jan 2019
Your welcome :)

Connectez-vous pour commenter.

Plus de réponses (2)

Luna
Luna le 3 Jan 2019
Hello Sena,
Try this below:
newA = A(~all((diff(A')' == 1)')',:);

5 commentaires

sena
sena le 3 Jan 2019
Hello Luna,
Thank you for answering my question. Your suggestion works absolutely fine if the entire row contains consecutive numbers. However, I need a condition that if there are more than let's say 3 consecutive numbers than that entire row needs to be removed from matrix A.
So for example, if matrix A is as follows:
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];
Then given the condition that all rows with 3 or more consecutive numbers are removed the resulting matrix A should look like:
A = [1 2 4 5 7
3 5 6 6 9
1 4 10 15 16];
Hopefully, I'm clearer this time around and thank you once again for your help.
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
Luna le 3 Jan 2019
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
sena le 3 Jan 2019
It works really well with example I used in my question and comment, however when I tried to apply it to my actual problem it only works for the first row and then it stops there. For example, I used augmented matrix A as:
A = [1 2 3 4 10 2 5
3 4 5 9 11 3 8
1 2 4 5 7 3 1
3 5 6 6 9 1 4
1 4 10 15 16 8 7];
But new matrix A looks like:
newA = [3 4 5 9 11 3 8
1 2 4 5 7 3 1
3 5 6 6 9 1 4
1 4 10 15 16 8 7]
and it still contains the second row from original matrix A which satisfies condition of having 3 consecutive numbers.
Additionally, since the proposed formula uses syntex 'sum' there might be cases which satisfy sum condition but are not actully 3 consecutive numbers. For example,
[3 4 7 10 11 4 5]
which gives us logicals
[1 0 1 1]
so this will be equal to 3 but it does not contain 3 consecutive numbers.
Luna
Luna le 3 Jan 2019
Please look at the function I have given in another answer.
Write your k -> any number of consecutive you wish and your A matrix.

Connectez-vous pour commenter.

Bruno Luong
Bruno Luong le 3 Jan 2019
Modifié(e) : Bruno Luong le 4 Jan 2019
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 commentaire

Luna
Luna le 3 Jan 2019
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by