本文介绍了查找索引,相交,然后在矩阵中排名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的矩阵:

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列.代码如下:

Of the above table, X and Y columns have been easily found with the help of Matlab code as provided by Sir Luis Mendo to my previous question.Code was as follows:

[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,但是我无法使用相交命令来找到.

Now i wish to find intersection set i.e. Z but i m not able to find using intersection command.

在比较X列和Z列时应进行进一步排名,以使两列中的元素最少,应该有最大数量的公共元素,并且每次应该从整个X列中发出公共元素以进行比较.进一步比较,以便在两个集合中元素都最少的情况下,可以找到最大公共元素.

Further Ranking is to be done on comparing columns X and Z in such a manner that with minimum of elements in both columns, maximum of common elements should be there and each time that common element should be emitted from the whole X column for further comparison so that again with minimum of elements in both sets max common elements can be found.

请帮助.

推荐答案

对于交集,只需使用a & a.':

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

这篇关于查找索引,相交,然后在矩阵中排名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:39