Filter löschen
Filter löschen

exstract consecutive duplicate element of array

3 Ansichten (letzte 30 Tage)
ADC
ADC am 25 Okt. 2018
Bearbeitet: Guillaume am 25 Okt. 2018
Dear Dear I've got the folowing problem Given an A matrix nxm i need to exstract consecutive duplicate elements of array
so I mean if for excample the row n=n+1 =n+2 I want to get a matrix B obtainet from A less the rows duplicate
  4 Kommentare
jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
I think you missed some ;'s there? Should it be
A = [1 2 3; 1 2 7; 3 4 9]
and the output
B = [1 2 3; 3 4 9]
?? I'm just guessing here
ADC
ADC am 25 Okt. 2018
yess

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Stephen23
Stephen23 am 25 Okt. 2018
>> A = [1,2,3;1,2,7;3,4,9;9,9,9;7,7,7]
A =
1 2 3
1 2 7
3 4 9
9 9 9
7 7 7
>> B = A([true;all(diff(A,1,1)~=0,2)],:)
B =
1 2 3
3 4 9
7 7 7

jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
A is your matrix. This line removes duplicates:
A([false diff(A)==0]) = [];
Or you could expand it to this, given your vague example from the comments
A =
1 2 3
1 2 7
3 4 9
_
mask = [ones(1,size(A,1));diff(A,1)] == 0;
A = A(sum(mask,2)==0,:)
A =
1 2 3
3 4 9
  2 Kommentare
ADC
ADC am 25 Okt. 2018
this is exsactly what I need but I whant that this check is done only for example a given coulumn, for example the coloumn 3
jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
Simple enough, just change this line
mask = [false;diff(A(:,3))==0]
or simply write it like this
mask = [false;diff(A(:,3))==0]
A = A(~mask,:)
The number, in this case 3, determines the column over which the check is made

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by