Create a relation matrix from a sequence (Matlab)

后端 未结 2 733
逝去的感伤
逝去的感伤 2021-01-17 04:02

I have a sequence S :

  S= \'ABCD\' % which means A

I want to convert S into a matrix M[i,j] which have to satisfy those con

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 04:45

    From your question it appears you want

    • 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
    

    Example result:

    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
    

提交回复
热议问题