从给定的字符串单元格数组创建关系矩阵(Matlab) [英] Create relation matrices from a given cell-array of strings (Matlab)

查看:85
本文介绍了从给定的字符串单元格数组创建关系矩阵(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个细胞阵列中有2个序列:

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

我想将Input_cell中的每个字符串转换为必须满足这些条件的矩阵M[i,j]:

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?

推荐答案

%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')

这篇关于从给定的字符串单元格数组创建关系矩阵(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆