0

The difference in this case from the previous cases is the length of each string the the Input-cell is the same. One of the previous questions is in different length, another case just transform from one string into a matrix

I have 3 sequences in a cell-array:

Input_cell= {'ABCD','ACDB', 'BCAD'}

S1= 'ABCD'  % which means  A<B<C<D
S2= 'ACDB'  % which means A<C<D<B  
S3= 'BCAD'  % which means B<C<A<D  

I want to convert each of the strings in the Input_cell into a matrix M (i-by-j) which has to satisfy these conditions:

M(i,j) and 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 = 'ABCD' (which means A<B<C<D), the M1 matrix will be expected as follows:



     A      B     C     D
 A  0.5    0.3   0.2   0.1 
 B  0.7    0.5    0    0.4
 C  0.8     1    0.5   0.1
 D  0.9    0.6   0.9   0.5

%If we have S2 = 'ACDB' (which means A<C<D<B), the M2 matrix will be expected as follows:

     A      B     C     D
 A  0.5    0.3   0.2   0.1 
 B  0.7    0.5   0.6   0.8
 C  0.8    0.4   0.5   0.1
 D  0.9    0.2   0.9   0.5 


% If we have S3 = 'BCAD' (which means B<C<A<D), the M3 matrix will be expected as follows:


     A      B     C     D
 A  0.5    0.6   0.7   0.1 
 B  0.4    0.5   0.2   0.3
 C  0.3    0.8   0.5   0.1
 D  0.9    0.7   0.9   0.5  

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

6
  • 1
    Also duplicate of: stackoverflow.com/questions/32744677/…, stackoverflow.com/questions/32882678/… and stackoverflow.com/questions/33011701/…. Commented Oct 12, 2015 at 14:15
  • 1
    Seems to me that none of the answer to these posts follow the conditions about the input's ordering Commented Oct 12, 2015 at 14:42
  • 1
    @BillBokeey In the question I marked as duplicate, both Luis and I asked for clarification on that point. Rather than elaborating, the OP opened several duplicates. Commented Oct 12, 2015 at 14:59
  • Thanks all for your comment! The difference in this case from the previous cases is the length of each string the the Input-cell is the same. One of the previous question is in different length, another one just from one string into a matrix. Should I delete this question or just keep it? Commented Oct 12, 2015 at 15:23
  • 1
    @beaker : yes, sorry, i saw that afterwards. Kgk : i think you got way enough material in all your question to answer that by yourself Commented Oct 12, 2015 at 16:47

1 Answer 1

1

I would do the following, even if it doesn't seem really optimized :

1) Fill your output matrix as if the order was the natural one (i.e. 'ABCD').

2) Apply a permutation to your matrix so that it satisfies your condition.

Codewise :

1)

  • Generate an upper diagonal matrix with 0.5 on the diagonal and sorted random generated values in [0,0.5] above the diagonal, for example :

A=diag([0.5 0.5 0.5 0.5])+sort(triu(0.5*rand(4),1),2);

Example of output :

A =

   0.500000000000000   0.084125649245764   0.108781654711410   0.277868971359693
                   0   0.500000000000000   0.092216833878827   0.125520923007868
                   0                   0   0.500000000000000   0.106015421266160
                   0                   0                   0   0.500000000000000
  • Fill the values of lower traingular block to match your constraint M(i,j)+M(j,i)=1 :

A=A+tril(1-transpose(A),-1);

output :

A =

   0.500000000000000   0.084125649245764   0.108781654711410   0.277868971359693
   0.915874350754236   0.500000000000000   0.092216833878827   0.125520923007868
   0.891218345288590   0.907783166121173   0.500000000000000   0.106015421266160
   0.722131028640307   0.874479076992132   0.893984578733840   0.500000000000000

2) Apply a permutation corresponding to the ordering of your input sequence.

NOTE : Characters are ordered alphabetically due to the way ASCII code is defined (see for example the outputs of 'A'<'B', or 'H'>'Z');

With that in mind, one can sort your input sequence in order to get the order it was in with the second output of MATLAB's sort function.

[Tmp,order]=sort(input);

Now one just has to calculate the input matrix you want :

A=A(order,order);

output, case input='ACDB' :

A =

   0.500000000000000   0.277868971359693   0.084125649245764   0.108781654711410
   0.722131028640307   0.500000000000000   0.874479076992132   0.893984578733840
   0.915874350754236   0.125520923007868   0.500000000000000   0.092216833878827
   0.891218345288590   0.106015421266160   0.907783166121173   0.500000000000000

output, case input='BCAD' :

A =

   0.500000000000000   0.891218345288590   0.907783166121173   0.106015421266160
   0.108781654711410   0.500000000000000   0.084125649245764   0.277868971359693
   0.092216833878827   0.915874350754236   0.500000000000000   0.125520923007868
   0.893984578733840   0.722131028640307   0.874479076992132   0.500000000000000
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your solution ! I am wondering that how can you handle the Input_cell to generate 3 matrices at the same time ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.