找到索引,交集然后在矩阵中排名?

时间:2014-03-23 13:57:36

标签: matlab

我有一个如下矩阵:

a = [1 0 0 0 0 0 0;
     1 1 0 0 0 0 0;
     1 0 1 0 0 0 0;
     1 1 0 1 1 0 0;
     1 1 0 1 1 0 0;
     1 0 1 0 0 1 1;
     1 0 1 0 0 1 1]

我希望创建以下表格:

X - For each Rows index of cols having 1
Y - For each cols index of rows having 1
Z - Intersection set


S.No      X               Y               Z       Rank(Comparing X & Z)
1        1          1,2,3,4,5,6,7        1                 I
2        1,2           2,4,5             2                 II
3        1,3           3,6,7             3                 II
4       1,2,4,5         4,5             4,5                III
5       1,2,4,5         4,5             4,5                III
6       1,3,6,7         6,7             6,7                III
7       1,3,6,7         6,7             6,7                III

在上表中,在Luis Mendo爵士提供给我之前的问题的Matlab代码的帮助下,很容易找到X和Y列。 代码如下:

[ii jj] = find(a); %// find row and col indices (ii and jj respectively)
rows = accumarray(ii,jj,[], @(v) {sort(v).'}); %'// group jj as per ii, and sort
cols = accumarray(jj,ii,[], @(v) {sort(v).'}); %'// group ii as per jj, and sort
rows{:}
cols{:}

现在我希望找到交集,即Z,但我无法找到使用交叉命令。

在比较X和Z列时,要进行进一步的排序,使得两列中的元素最少,每个公共元素的最大值应该存在,并且每次应该从整个X列发出公共元素进一步比较,以便两个集合中的元素最少,可以找到最大公共元素。

请帮忙。

1 个答案:

答案 0 :(得分:0)

对于交叉点,只需使用a & a.'

[ii jj] = find(a & a.');
z = accumarray(ii,jj,[], @(v) {sort(v).'});