【问题标题】:Create relation matrices from a given cell-array of strings (Matlab)从给定的字符串元胞数组创建关系矩阵(Matlab)
【发布时间】:2015-10-01 08:17:41
【问题描述】:

我在一个单元阵列中有 2 个序列:

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

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

       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   

如何从给定的序列单元阵列创建上述矩阵?

【问题讨论】:

  • 你有没有天真地试图在那边展示?

标签: matlab matrix cell-array


【解决方案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')

【讨论】:

  • 如何从给定的单元格数组 Input-cell = { 'ABC', 'ACB'} 一次生成 2 个矩阵?
  • 使用循环并遍历。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2015-05-29
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多