How to make comparison between paired elements?

Hi, I would like to compare pair of adjacent elements of array. For example:
if
A=[1;
2;
1;
3;
2;
4]
In this case the pairs are (1 2) (1 3) (2 4)... So in this case I have "unique" pairs.
But for example:
A=[1;
2;
1;
2;
2;
4]
In this case the pairs are (1 2) (1 2) (2 4) .... In this case I don't have "unique" pairs.
How can I make the comparisson of pairs, considering the pair is form with the adjacent element.
thanks

 채택된 답변

Star Strider
Star Strider 2014년 6월 19일

0 개 추천

A=[1;
2;
1;
2;
2;
4]
Ar = reshape(A, 2, [])';
UA = unique(Ar,'rows')
The ‘UA’ matrix rows are the unique adjacent elements of your ‘A’ vector.

댓글 수: 4

Patty
Patty 2014년 6월 19일
But, for example... If I have the matrix
A=[1;2;2;1;3;4]
The pairs are (1 2) (2 1) (3 4) .... In this case the first and second pair are considered the same because both has the same elements inside, it doesnt matter that is different position.
Rehsape to Ar as shown, then sort your rows before applying unique:
A=[1;
2;
2;
1;
3;
4];
Ar = reshape(A, 2, [])';
Ars = sort(Ar, 2);
UA = unique(Ars,'rows')
I got it using your suggestions... Thanks so much!.. Just a little change.
D=reshape(A,2,[]);
F=sum(D);
anyRepeated = length(unique(F) < length(F));
if anyRepeated<length(F)
passFlag2=0;
else
passFlag2=1;
end
I just copied here, in case somebody else needed.. :)
My pleasure!
And thank you arich82.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2014년 6월 19일

댓글:

2014년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by