根据序列创建关系矩阵(Matlab) [英] Create a relation matrix from a sequence (Matlab)

查看:259
本文介绍了根据序列创建关系矩阵(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个序列S:

  S= 'ABCD' % which means A<B<C<D

我想将S转换为必须满足这些条件的矩阵M [i,j]:

I want to convert S into a matrix M[i,j] which have 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]

例如:如果我们有S ='ABCD',则期望M矩阵如下:

For example: if we have S = 'ABCD', the M matrix will be expected as follows:

      A      B    C    D
   A  o.5  0.25  0.2   0.1
   B  0.75 0.5   0.35  0.15
   C  0.8  0.65  0.5   0.4
   D  0.9  0.85  0.6   0.5

如何从给定序列创建上述类型的矩阵?

How to create that kind of above matrix from a given sequence ?

推荐答案

从您的问题来看,您想要的是

From your question it appears you want

  • 用在间隔(0,0.5)上均匀分布的随机条目填充矩阵的下部(以便满足您问题中的条件4);
  • 然后根据条件3计算上部;和
  • 对角线由条件2确定.
  • to fill the lower part of the matrix with random entries uniformly distributed on the interval (0,0.5) (so that condition 4 in your question be satisfied);
  • the upper part is then computed according to condition 3; and
  • the diagonal is determined by condition 2.

您可以按照以下步骤进行操作:

You can do that as follows:

n = 4; %// size
M = NaN(n); %// preallocate
M(1:n+1:end) = 0.5; %// fill diagonal
ind_lower = tril(true(n), -1); %// logical index for lower part
M(ind_lower) = 0.5*rand(n*(n-1)/2, 1); %// fill lower part
M_aux = NaN(n); %// auxiliary variable to fill upper part
M_aux(ind_lower) = 1-M(ind_lower).';
M_aux = M_aux.';
M(ind_lower.') = M_aux(ind_lower.'); %// fill upper part

示例结果:

M =
    0.5000    0.5214    0.7573    0.5999
    0.4786    0.5000    0.9291    0.7891
    0.2427    0.0709    0.5000    0.5421
    0.4001    0.2109    0.4579    0.5000

这篇关于根据序列创建关系矩阵(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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