Create relation matrices from a given cell-array of strings (Matlab)

喜夏-厌秋 提交于 2019-12-23 05:08:00

问题


I have 2 sequences in a cell-array :

 Input_cell= {'ABC','ACB'}
 S1= 'ABC' % which means A<B<C
 S2= 'ACB' % which means A<C<B

I want to convert each of the strings in the Input_cell into a matrix M[i,j] which has to satisfy those conditions :

       M[i,j] , M[j,i] are random
       M[i,i] =0.5
       M[i,j] + M[j,i] = 1
       M[i,j] < M[j,i] % For example: if A<B then M[A,B] < M[B,A]


%// For example: if we have S1 = 'ABC'  (which means `A<B<C`), the M1 matrix will  be expected as follows:

     A      B    C    
  A  0.5    0    0   
  B  1     0.5   0  
  C  1      1   0.5   


%// If we have S2 = 'ACB' (which means `A<C<B`), the M2 matrix will be expected as follows:
     A      B    C    
  A  0.5    0    0   
  B  1     0.5   1  
  C  1      0   0.5   

How to create that kind of above matrices from a given cell-array of sequences?


回答1:


%get the ordering, where '312' would mean that A is largest, then C, then B
[~,k]=sort(S2);
%compare each pair
bsxfun(@(a,b)(a<b)+0.5*(a==b),k,k')


来源:https://stackoverflow.com/questions/32882678/create-relation-matrices-from-a-given-cell-array-of-strings-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!