How to compare each four rows by each other?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello,
Suppose we have 8 rows where each element in a row is either zero or one. I want to compare rows to count differences in this way:
row1 with row2 ; row1 with row 3; row1 with row4; row2 with rows 3; row2 with row4; row3 with row4.
So how can I do that in repetition manner to repeat doing that for row5,6,7, and 8 together. note that the number of different elements between any two rows should be stored in a matrix where in our case here we should have 6 rows in the differences matrix where each row store the number of differences between two rows in the main matrix.
Regards,
댓글 수: 0
채택된 답변
Star Strider
2018년 9월 16일
If you have the Statistics and Machine Learning Toolbox, use the pdist (link) function, specifying the 'hamming' distance measure:
data = randi([0 1], 8, 5) % Create Data
d = pdist(data, 'hamming');
DistancesP = squareform(d) % Percentage Differences
DistancesA = squareform(d) * size(data,2) % Number Of Elements That Differ
produces:
data =
1 1 1 1 0
0 0 1 0 0
0 0 1 0 0
0 0 0 1 0
0 1 1 1 1
0 0 0 0 1
1 0 1 1 1
1 0 0 1 1
DistancesA =
0 3 3 3 2 5 2 3
3 0 0 2 3 2 3 4
3 0 0 2 3 2 3 4
3 2 2 0 3 2 3 2
2 3 3 3 0 3 2 3
5 2 2 2 3 0 3 2
2 3 3 3 2 3 0 1
3 4 4 2 3 2 1 0
댓글 수: 0
추가 답변 (1개)
ahmed nebli
2018년 9월 16일
i prpose this algorithm: u define a variable m=4, and n=1, n is the number of the row, each time u search for your diffrences between rows (+-n), then n=n+1 and u compare rows (m-n) and n=n+1, and so on ... each time u store it in an external matrix A ( like u said), after that m=m+4 and u repeat the loop
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Hamming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!