How to compare a matrix rows such as:
이전 댓글 표시
How to compare a matrix rows such as:
A=[
7 8 0 5 6 0 1 2 3 4
7 8 0 5 6 0 1 2 3 5
7 8 0 5 6 0 1 2 3 6
7 8 0 5 6 0 1 2 3 7
7 8 0 5 6 0 1 2 3 8
7 8 0 5 6 0 1 2 4 5
7 8 0 5 6 0 1 2 4 6
]
I want to get the results
B=[7 8 0 5 6 0 1 2 3 4] Each row 1-8 each appear once
Who can help me to discuss this issue with me?
댓글 수: 5
Stephen23
2015년 1월 13일
This does not really explain anything. If there is a method to this, then you need to explain it clearly for us.
This is still a poorly defined problem, although I am sure that it is quite easy to solve. It seems that you wish to exclude any row with repeated elements (your first and second points), but what is the rule for excluding A(4,:) and A(5,:)?
- you compare A(4,[1,2]) with A(2,[3,4]), but why row 2? Why not row 1 or any other row? How do I know that I should be comparing to row 2?
- you give two examples that show that order and position is unimportant ( A(4,[1,2]) vs A(2,[3,4]) and A(5,[1,2]) vs A(2[4,3]). Is the position or number of compared elements important? Can I compare one element? Or three? Do they need to be adjacent?
- In your examples you have different columns with zeros exclusively:
[7,8,0,5,6,0,1,2,3,4] % your original question
[1,0,2,0,3,4,5,6,7,8] % your comment to my first question.
Which of these is correct? Are the zeros significant?
Wang
2015년 1월 13일
Given the matrix A from your comment above:
A = [3 4 0 1 2 0 5 6 7 8;...
1 2 0 3 4 0 5 6 7 8;...
1 2 0 1 2 0 5 6 7 8;...
2 1 0 4 3 0 5 6 7 8];
We can try your proposed algorithm:
- row 1: repeated blocks on same row? no -> continue.
- row 1: repeated blocks from previous rows? no previous blocks -> continue.
- row 2: repeated blocks on same row? no -> continue.
- row 2: repeated blocks from previous rows? [1,2] is also in row 1 -> remove row 2.
And yet you give row 2 as the only remaining row in your solution.
So we have a contradiction between your explanation and the desired output that you give. You say that you do not want rows to contain blocks that are repeats of blocks from previous rows (which row 2 has) and yet you give row 2 as the only remaining line in your proposed output.
Also all rows repeat the block [5,6,7,8], so really row 1 can be the only remaining row, not row 2. Or, if we restrict ourselves to comparing only the first two blocks on each row, then as shown row 2 anyway repeats a block from row 1, so must be excluded.
Your explanation is not adequate, or is inconsistent. Please explain it more carefully.
답변 (4개)
Try this:
>> [A(1,1:8),unique(A(:,9)).']
ans =
7 8 0 5 6 0 1 2 3 4
This takes the first eight elements of the first row, and concatenates these with the unique elements of the ninth column, which matches your example output. It seems that your example does not reference the tenth column: is this intentional?
댓글 수: 1
Ced
2015년 1월 12일
Can we assume that the numbers are sorted as in your example?
Honglei Chen
2015년 1월 12일
B = sort(A,2);
A(ismember(B(:,3:end),1:8,'rows'),:)
Stephen23
2015년 1월 12일
You explanations are not very clear, but it appears that you want an operation that returns each unique row of your input matrix A. This can be done very easily using the unique function. Read its documentation carefully and you will find the second argument quite useful for your code:
>> A = [1,2,3;4,5,6;1,2,3;1,2,3;7,8,9;4,5,6] % six rows
A =
1 2 3
4 5 6
1 2 3
1 2 3
7 8 9
4 5 6
>> B = unique(A,'rows') % finds all three unique rows
B =
1 2 3
4 5 6
7 8 9
Ced
2015년 1월 12일
As an alternative, I came up with this code which seems to do the same thing.
[ ~, ind_sort ] = sort(A(:,1));
B = A(ind_sort,:);
B([false; (sum(B(1:end-1,:)==B(2:end,:),2)==size(A,2)) ],:) = [];
From a short test, it's faster than unique by a factor of at least 2. If your matrix is already sorted, you can leave out the first two lines and simply set B = A;
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!