How to compare a matrix rows such as:
4 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 16 Jan 2015
Edited: Stephen23
on 16 Jan 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.
Answers (4)
Stephen23
on 11 Jan 2015
Edited: Stephen23
on 11 Jan 2015
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 Comment
Stephen23
on 12 Jan 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
0 Comments
Ced
on 12 Jan 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;
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!