How to compare a matrix rows such as:

2 ビュー (過去 30 日間)
Wang
Wang 2015 年 1 月 11 日
編集済み: Stephen23 2015 年 1 月 16 日
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 件のコメント
Wang
Wang 2015 年 1 月 13 日
Numbers 1-8 divided into three groups
For example : [3 4 0 1 2 0 5 6 7 8]
First block is 3,4
Second block is 1,2
Third block is 5,6,7,8
Block combination can not be repeated
For example : %A(1,1:2) and A(2,3:4) and A(4,3:4) is repeated
The final result for the B matrix
How can I to do this?
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
]; % four row
B=
[
1 2 0 3 4 0 5 6 7 8
];
Stephen23
Stephen23 2015 年 1 月 16 日
編集済み: Stephen23 2015 年 1 月 16 日
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 件)

Stephen23
Stephen23 2015 年 1 月 11 日
編集済み: Stephen23 2015 年 1 月 11 日
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
Ced 2015 年 1 月 12 日
Can we assume that the numbers are sorted as in your example?

サインインしてコメントする。


Honglei Chen
Honglei Chen 2015 年 1 月 12 日
B = sort(A,2);
A(ismember(B(:,3:end),1:8,'rows'),:)

Stephen23
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
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;

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by