How to compare a matrix rows such as:
Mostra commenti meno recenti
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 Commenti
Stephen23
il 13 Gen 2015
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
il 13 Gen 2015
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.
Risposte (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 Commento
Ced
il 12 Gen 2015
Can we assume that the numbers are sorted as in your example?
Honglei Chen
il 12 Gen 2015
B = sort(A,2);
A(ismember(B(:,3:end),1:8,'rows'),:)
Stephen23
il 12 Gen 2015
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
il 12 Gen 2015
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;
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!